@@ -59,7 +59,7 @@ TargetInfo *Target;
59
59
static void or32le (uint8_t *P, int32_t V) { write32le (P, read32le (P) | V); }
60
60
static void or32be (uint8_t *P, int32_t V) { write32be (P, read32be (P) | V); }
61
61
62
- template <class ELFT > static std::string getErrorLoc (uint8_t *Loc) {
62
+ template <class ELFT > static std::string getErrorLoc (const uint8_t *Loc) {
63
63
for (InputSectionBase *D : InputSections) {
64
64
auto *IS = dyn_cast_or_null<InputSection>(D);
65
65
if (!IS || !IS->OutSec )
@@ -72,7 +72,7 @@ template <class ELFT> static std::string getErrorLoc(uint8_t *Loc) {
72
72
return " " ;
73
73
}
74
74
75
- static std::string getErrorLocation (uint8_t *Loc) {
75
+ static std::string getErrorLocation (const uint8_t *Loc) {
76
76
switch (Config->EKind ) {
77
77
case ELF32LEKind:
78
78
return getErrorLoc<ELF32LE>(Loc);
@@ -119,7 +119,8 @@ namespace {
119
119
class X86TargetInfo final : public TargetInfo {
120
120
public:
121
121
X86TargetInfo ();
122
- RelExpr getRelExpr (uint32_t Type, const SymbolBody &S) const override ;
122
+ RelExpr getRelExpr (uint32_t Type, const SymbolBody &S,
123
+ const uint8_t *Loc) const override ;
123
124
int64_t getImplicitAddend (const uint8_t *Buf, uint32_t Type) const override ;
124
125
void writeGotPltHeader (uint8_t *Buf) const override ;
125
126
uint32_t getDynRel (uint32_t Type) const override ;
@@ -143,7 +144,8 @@ class X86TargetInfo final : public TargetInfo {
143
144
template <class ELFT > class X86_64TargetInfo final : public TargetInfo {
144
145
public:
145
146
X86_64TargetInfo ();
146
- RelExpr getRelExpr (uint32_t Type, const SymbolBody &S) const override ;
147
+ RelExpr getRelExpr (uint32_t Type, const SymbolBody &S,
148
+ const uint8_t *Loc) const override ;
147
149
bool isPicRel (uint32_t Type) const override ;
148
150
bool isTlsLocalDynamicRel (uint32_t Type) const override ;
149
151
bool isTlsInitialExecRel (uint32_t Type) const override ;
@@ -171,13 +173,15 @@ class PPCTargetInfo final : public TargetInfo {
171
173
public:
172
174
PPCTargetInfo ();
173
175
void relocateOne (uint8_t *Loc, uint32_t Type, uint64_t Val) const override ;
174
- RelExpr getRelExpr (uint32_t Type, const SymbolBody &S) const override ;
176
+ RelExpr getRelExpr (uint32_t Type, const SymbolBody &S,
177
+ const uint8_t *Loc) const override ;
175
178
};
176
179
177
180
class PPC64TargetInfo final : public TargetInfo {
178
181
public:
179
182
PPC64TargetInfo ();
180
- RelExpr getRelExpr (uint32_t Type, const SymbolBody &S) const override ;
183
+ RelExpr getRelExpr (uint32_t Type, const SymbolBody &S,
184
+ const uint8_t *Loc) const override ;
181
185
void writePlt (uint8_t *Buf, uint64_t GotPltEntryAddr, uint64_t PltEntryAddr,
182
186
int32_t Index, unsigned RelOff) const override ;
183
187
void relocateOne (uint8_t *Loc, uint32_t Type, uint64_t Val) const override ;
@@ -186,7 +190,8 @@ class PPC64TargetInfo final : public TargetInfo {
186
190
class AArch64TargetInfo final : public TargetInfo {
187
191
public:
188
192
AArch64TargetInfo ();
189
- RelExpr getRelExpr (uint32_t Type, const SymbolBody &S) const override ;
193
+ RelExpr getRelExpr (uint32_t Type, const SymbolBody &S,
194
+ const uint8_t *Loc) const override ;
190
195
bool isPicRel (uint32_t Type) const override ;
191
196
bool isTlsInitialExecRel (uint32_t Type) const override ;
192
197
void writeGotPlt (uint8_t *Buf, const SymbolBody &S) const override ;
@@ -206,13 +211,15 @@ class AMDGPUTargetInfo final : public TargetInfo {
206
211
public:
207
212
AMDGPUTargetInfo ();
208
213
void relocateOne (uint8_t *Loc, uint32_t Type, uint64_t Val) const override ;
209
- RelExpr getRelExpr (uint32_t Type, const SymbolBody &S) const override ;
214
+ RelExpr getRelExpr (uint32_t Type, const SymbolBody &S,
215
+ const uint8_t *Loc) const override ;
210
216
};
211
217
212
218
class ARMTargetInfo final : public TargetInfo {
213
219
public:
214
220
ARMTargetInfo ();
215
- RelExpr getRelExpr (uint32_t Type, const SymbolBody &S) const override ;
221
+ RelExpr getRelExpr (uint32_t Type, const SymbolBody &S,
222
+ const uint8_t *Loc) const override ;
216
223
bool isPicRel (uint32_t Type) const override ;
217
224
uint32_t getDynRel (uint32_t Type) const override ;
218
225
int64_t getImplicitAddend (const uint8_t *Buf, uint32_t Type) const override ;
@@ -233,7 +240,8 @@ class ARMTargetInfo final : public TargetInfo {
233
240
template <class ELFT > class MipsTargetInfo final : public TargetInfo {
234
241
public:
235
242
MipsTargetInfo ();
236
- RelExpr getRelExpr (uint32_t Type, const SymbolBody &S) const override ;
243
+ RelExpr getRelExpr (uint32_t Type, const SymbolBody &S,
244
+ const uint8_t *Loc) const override ;
237
245
int64_t getImplicitAddend (const uint8_t *Buf, uint32_t Type) const override ;
238
246
bool isPicRel (uint32_t Type) const override ;
239
247
uint32_t getDynRel (uint32_t Type) const override ;
@@ -353,7 +361,8 @@ X86TargetInfo::X86TargetInfo() {
353
361
TrapInstr = 0xcccccccc ;
354
362
}
355
363
356
- RelExpr X86TargetInfo::getRelExpr (uint32_t Type, const SymbolBody &S) const {
364
+ RelExpr X86TargetInfo::getRelExpr (uint32_t Type, const SymbolBody &S,
365
+ const uint8_t *Loc) const {
357
366
switch (Type) {
358
367
case R_386_8:
359
368
case R_386_16:
@@ -376,6 +385,24 @@ RelExpr X86TargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const {
376
385
return R_GOT;
377
386
case R_386_GOT32:
378
387
case R_386_GOT32X:
388
+ // These relocations can be calculated in two different ways.
389
+ // Usual calculation is G + A - GOT what means an offset in GOT table
390
+ // (R_GOT_FROM_END). When instruction pointed by relocation has no base
391
+ // register, then relocations can be used when PIC code is disabled. In that
392
+ // case calculation is G + A, it resolves to an address of entry in GOT
393
+ // (R_GOT) and not an offset.
394
+ //
395
+ // To check that instruction has no base register we scan ModR/M byte.
396
+ // See "Table 2-2. 32-Bit Addressing Forms with the ModR/M Byte"
397
+ // (http://www.intel.com/content/dam/www/public/us/en/documents/manuals/
398
+ // 64-ia-32-architectures-software-developer-instruction-set-reference-manual-325383.pdf)
399
+ if ((Loc[-1 ] & 0xc7 ) != 0x5 )
400
+ return R_GOT_FROM_END;
401
+ if (Config->Pic )
402
+ error (toString (S.File ) + " : relocation " + toString (Type) + " against '" +
403
+ S.getName () +
404
+ " ' without base register can not be used when PIC enabled" );
405
+ return R_GOT;
379
406
case R_386_TLS_GOTIE:
380
407
return R_GOT_FROM_END;
381
408
case R_386_GOTOFF:
@@ -654,8 +681,8 @@ template <class ELFT> X86_64TargetInfo<ELFT>::X86_64TargetInfo() {
654
681
}
655
682
656
683
template <class ELFT >
657
- RelExpr X86_64TargetInfo<ELFT>::getRelExpr(uint32_t Type,
658
- const SymbolBody &S ) const {
684
+ RelExpr X86_64TargetInfo<ELFT>::getRelExpr(uint32_t Type, const SymbolBody &S,
685
+ const uint8_t *Loc ) const {
659
686
switch (Type) {
660
687
case R_X86_64_8:
661
688
case R_X86_64_16:
@@ -1093,7 +1120,8 @@ void PPCTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1093
1120
}
1094
1121
}
1095
1122
1096
- RelExpr PPCTargetInfo::getRelExpr (uint32_t Type, const SymbolBody &S) const {
1123
+ RelExpr PPCTargetInfo::getRelExpr (uint32_t Type, const SymbolBody &S,
1124
+ const uint8_t *Loc) const {
1097
1125
switch (Type) {
1098
1126
case R_PPC_REL24:
1099
1127
case R_PPC_REL32:
@@ -1142,7 +1170,8 @@ uint64_t getPPC64TocBase() {
1142
1170
return TocVA + PPC64TocOffset;
1143
1171
}
1144
1172
1145
- RelExpr PPC64TargetInfo::getRelExpr (uint32_t Type, const SymbolBody &S) const {
1173
+ RelExpr PPC64TargetInfo::getRelExpr (uint32_t Type, const SymbolBody &S,
1174
+ const uint8_t *Loc) const {
1146
1175
switch (Type) {
1147
1176
default :
1148
1177
return R_ABS;
@@ -1290,8 +1319,8 @@ AArch64TargetInfo::AArch64TargetInfo() {
1290
1319
TcbSize = 16 ;
1291
1320
}
1292
1321
1293
- RelExpr AArch64TargetInfo::getRelExpr (uint32_t Type,
1294
- const SymbolBody &S ) const {
1322
+ RelExpr AArch64TargetInfo::getRelExpr (uint32_t Type, const SymbolBody &S,
1323
+ const uint8_t *Loc ) const {
1295
1324
switch (Type) {
1296
1325
default :
1297
1326
return R_ABS;
@@ -1634,7 +1663,8 @@ void AMDGPUTargetInfo::relocateOne(uint8_t *Loc, uint32_t Type,
1634
1663
}
1635
1664
}
1636
1665
1637
- RelExpr AMDGPUTargetInfo::getRelExpr (uint32_t Type, const SymbolBody &S) const {
1666
+ RelExpr AMDGPUTargetInfo::getRelExpr (uint32_t Type, const SymbolBody &S,
1667
+ const uint8_t *Loc) const {
1638
1668
switch (Type) {
1639
1669
case R_AMDGPU_ABS32:
1640
1670
case R_AMDGPU_ABS64:
@@ -1671,7 +1701,8 @@ ARMTargetInfo::ARMTargetInfo() {
1671
1701
NeedsThunks = true ;
1672
1702
}
1673
1703
1674
- RelExpr ARMTargetInfo::getRelExpr (uint32_t Type, const SymbolBody &S) const {
1704
+ RelExpr ARMTargetInfo::getRelExpr (uint32_t Type, const SymbolBody &S,
1705
+ const uint8_t *Loc) const {
1675
1706
switch (Type) {
1676
1707
default :
1677
1708
return R_ABS;
@@ -2065,8 +2096,8 @@ template <class ELFT> MipsTargetInfo<ELFT>::MipsTargetInfo() {
2065
2096
}
2066
2097
2067
2098
template <class ELFT >
2068
- RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type,
2069
- const SymbolBody &S ) const {
2099
+ RelExpr MipsTargetInfo<ELFT>::getRelExpr(uint32_t Type, const SymbolBody &S,
2100
+ const uint8_t *Loc ) const {
2070
2101
// See comment in the calculateMipsRelChain.
2071
2102
if (ELFT::Is64Bits || Config->MipsN32Abi )
2072
2103
Type &= 0xff ;
0 commit comments