@@ -167,6 +167,10 @@ class BinaryFunction {
167167 // / List of relocations associated with data in the constant island
168168 std::map<uint64_t , Relocation> Relocations;
169169
170+ // / Set true if constant island contains dynamic relocations, which may
171+ // / happen if binary is linked with -z notext option.
172+ bool HasDynamicRelocations{false };
173+
170174 // / Offsets in function that are data values in a constant island identified
171175 // / after disassembling
172176 std::map<uint64_t , MCSymbol *> Offsets;
@@ -349,6 +353,9 @@ class BinaryFunction {
349353 // / This attribute is only valid when hasCFG() == true.
350354 bool HasCanonicalCFG{true };
351355
356+ // / True if another function body was merged into this one.
357+ bool HasFunctionsFoldedInto{false };
358+
352359 // / Name for the section this function code should reside in.
353360 std::string CodeSectionName;
354361
@@ -1226,96 +1233,11 @@ class BinaryFunction {
12261233 return InputOffsetToAddressMap;
12271234 }
12281235
1229- void addRelocationAArch64 (uint64_t Offset, MCSymbol *Symbol, uint64_t RelType,
1230- uint64_t Addend, uint64_t Value, bool IsCI) {
1231- std::map<uint64_t , Relocation> &Rels =
1232- (IsCI) ? Islands->Relocations : Relocations;
1233- switch (RelType) {
1234- case ELF::R_AARCH64_ABS64:
1235- case ELF::R_AARCH64_ABS32:
1236- case ELF::R_AARCH64_ABS16:
1237- case ELF::R_AARCH64_ADD_ABS_LO12_NC:
1238- case ELF::R_AARCH64_ADR_GOT_PAGE:
1239- case ELF::R_AARCH64_ADR_PREL_LO21:
1240- case ELF::R_AARCH64_ADR_PREL_PG_HI21:
1241- case ELF::R_AARCH64_ADR_PREL_PG_HI21_NC:
1242- case ELF::R_AARCH64_LD64_GOT_LO12_NC:
1243- case ELF::R_AARCH64_LDST8_ABS_LO12_NC:
1244- case ELF::R_AARCH64_LDST16_ABS_LO12_NC:
1245- case ELF::R_AARCH64_LDST32_ABS_LO12_NC:
1246- case ELF::R_AARCH64_LDST64_ABS_LO12_NC:
1247- case ELF::R_AARCH64_LDST128_ABS_LO12_NC:
1248- case ELF::R_AARCH64_TLSDESC_ADD_LO12:
1249- case ELF::R_AARCH64_TLSDESC_ADR_PAGE21:
1250- case ELF::R_AARCH64_TLSDESC_ADR_PREL21:
1251- case ELF::R_AARCH64_TLSDESC_LD64_LO12:
1252- case ELF::R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21:
1253- case ELF::R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC:
1254- case ELF::R_AARCH64_MOVW_UABS_G0:
1255- case ELF::R_AARCH64_MOVW_UABS_G0_NC:
1256- case ELF::R_AARCH64_MOVW_UABS_G1:
1257- case ELF::R_AARCH64_MOVW_UABS_G1_NC:
1258- case ELF::R_AARCH64_MOVW_UABS_G2:
1259- case ELF::R_AARCH64_MOVW_UABS_G2_NC:
1260- case ELF::R_AARCH64_MOVW_UABS_G3:
1261- case ELF::R_AARCH64_PREL16:
1262- case ELF::R_AARCH64_PREL32:
1263- case ELF::R_AARCH64_PREL64:
1264- Rels[Offset] = Relocation{Offset, Symbol, RelType, Addend, Value};
1265- return ;
1266- case ELF::R_AARCH64_CALL26:
1267- case ELF::R_AARCH64_JUMP26:
1268- case ELF::R_AARCH64_TSTBR14:
1269- case ELF::R_AARCH64_CONDBR19:
1270- case ELF::R_AARCH64_TLSDESC_CALL:
1271- case ELF::R_AARCH64_TLSLE_ADD_TPREL_HI12:
1272- case ELF::R_AARCH64_TLSLE_ADD_TPREL_LO12_NC:
1273- return ;
1274- default :
1275- llvm_unreachable (" Unexpected AArch64 relocation type in code" );
1276- }
1277- }
1278-
1279- void addRelocationX86 (uint64_t Offset, MCSymbol *Symbol, uint64_t RelType,
1280- uint64_t Addend, uint64_t Value) {
1281- switch (RelType) {
1282- case ELF::R_X86_64_8:
1283- case ELF::R_X86_64_16:
1284- case ELF::R_X86_64_32:
1285- case ELF::R_X86_64_32S:
1286- case ELF::R_X86_64_64:
1287- case ELF::R_X86_64_PC8:
1288- case ELF::R_X86_64_PC32:
1289- case ELF::R_X86_64_PC64:
1290- case ELF::R_X86_64_GOTPCRELX:
1291- case ELF::R_X86_64_REX_GOTPCRELX:
1292- Relocations[Offset] = Relocation{Offset, Symbol, RelType, Addend, Value};
1293- return ;
1294- case ELF::R_X86_64_PLT32:
1295- case ELF::R_X86_64_GOTPCREL:
1296- case ELF::R_X86_64_TPOFF32:
1297- case ELF::R_X86_64_GOTTPOFF:
1298- return ;
1299- default :
1300- llvm_unreachable (" Unexpected x86 relocation type in code" );
1301- }
1302- }
1303-
13041236 // / Register relocation type \p RelType at a given \p Address in the function
13051237 // / against \p Symbol.
13061238 // / Assert if the \p Address is not inside this function.
13071239 void addRelocation (uint64_t Address, MCSymbol *Symbol, uint64_t RelType,
1308- uint64_t Addend, uint64_t Value) {
1309- assert (Address >= getAddress () && Address < getAddress () + getMaxSize () &&
1310- " address is outside of the function" );
1311- uint64_t Offset = Address - getAddress ();
1312- if (BC.isAArch64 ()) {
1313- return addRelocationAArch64 (Offset, Symbol, RelType, Addend, Value,
1314- isInConstantIsland (Address));
1315- }
1316-
1317- return addRelocationX86 (Offset, Symbol, RelType, Addend, Value);
1318- }
1240+ uint64_t Addend, uint64_t Value);
13191241
13201242 // / Return the name of the section this function originated from.
13211243 std::optional<StringRef> getOriginSectionName () const {
@@ -1407,6 +1329,9 @@ class BinaryFunction {
14071329 // / Return true if the body of the function was merged into another function.
14081330 bool isFolded () const { return FoldedIntoFunction != nullptr ; }
14091331
1332+ // / Return true if other functions were folded into this one.
1333+ bool hasFunctionsFoldedInto () const { return HasFunctionsFoldedInto; }
1334+
14101335 // / If this function was folded, return the function it was folded into.
14111336 BinaryFunction *getFoldedIntoFunction () const { return FoldedIntoFunction; }
14121337
@@ -1460,8 +1385,6 @@ class BinaryFunction {
14601385
14611386 ArrayRef<uint8_t > getLSDATypeIndexTable () const { return LSDATypeIndexTable; }
14621387
1463- const LabelsMapType &getLabels () const { return Labels; }
1464-
14651388 IslandInfo &getIslandInfo () {
14661389 assert (Islands && " function expected to have constant islands" );
14671390 return *Islands;
@@ -1770,6 +1693,9 @@ class BinaryFunction {
17701693
17711694 void setFolded (BinaryFunction *BF) { FoldedIntoFunction = BF; }
17721695
1696+ // / Indicate that another function body was merged with this function.
1697+ void setHasFunctionsFoldedInto () { HasFunctionsFoldedInto = true ; }
1698+
17731699 BinaryFunction &setPersonalityFunction (uint64_t Addr) {
17741700 assert (!PersonalityFunction && " can't set personality function twice" );
17751701 PersonalityFunction = BC.getOrCreateGlobalSymbol (Addr, " FUNCat" );
@@ -1828,7 +1754,7 @@ class BinaryFunction {
18281754
18291755 // / Returns if this function is a child of \p Other function.
18301756 bool isChildOf (const BinaryFunction &Other) const {
1831- return llvm::is_contained ( ParentFragments, &Other);
1757+ return ParentFragments. contains ( &Other);
18321758 }
18331759
18341760 // / Set the profile data for the number of times the function was called.
@@ -1938,6 +1864,28 @@ class BinaryFunction {
19381864 return Symbol;
19391865 }
19401866
1867+ // / Support dynamic relocations in constant islands, which may happen if
1868+ // / binary is linked with -z notext option.
1869+ void markIslandDynamicRelocationAtAddress (uint64_t Address) {
1870+ if (!isInConstantIsland (Address)) {
1871+ errs () << " BOLT-ERROR: dynamic relocation found for text section at 0x"
1872+ << Twine::utohexstr (Address) << " \n " ;
1873+ exit (1 );
1874+ }
1875+
1876+ // Mark island to have dynamic relocation
1877+ Islands->HasDynamicRelocations = true ;
1878+
1879+ // Create island access, so we would emit the label and
1880+ // move binary data during updateOutputValues, making us emit
1881+ // dynamic relocation with the right offset value.
1882+ getOrCreateIslandAccess (Address);
1883+ }
1884+
1885+ bool hasDynamicRelocationAtIsland () const {
1886+ return !!(Islands && Islands->HasDynamicRelocations );
1887+ }
1888+
19411889 // / Called by an external function which wishes to emit references to constant
19421890 // / island symbols of this function. We create a proxy for it, so we emit
19431891 // / separate symbols when emitting our constant island on behalf of this other
0 commit comments