llvm.org GIT mirror llvm / ce20559
[mips] Adds support for R_MIPS_26, HIGHER, HIGHEST relocations in RuntimeDyld. Reviewers: sdardis Subscribers: jaydeep, bhushan, llvm-commits Differential Revision: https://reviews.llvm.org/D38314 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@316287 91177308-0d34-0410-b5e6-96231b3b80d8 Nitesh Jain 3 years ago
6 changed file(s) with 122 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
877877 // and stubs for branches Thumb - ARM and ARM - Thumb.
878878 writeBytesUnaligned(0xe51ff004, Addr, 4); // ldr pc, [pc, #-4]
879879 return Addr + 4;
880 } else if (IsMipsO32ABI) {
880 } else if (IsMipsO32ABI || IsMipsN32ABI) {
881881 // 0: 3c190000 lui t9,%hi(addr).
882882 // 4: 27390000 addiu t9,t9,%lo(addr).
883883 // 8: 03200008 jr t9.
885885 const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
886886 const unsigned NopInstr = 0x0;
887887 unsigned JrT9Instr = 0x03200008;
888 if ((AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_32R6)
889 JrT9Instr = 0x03200009;
888 if ((AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_32R6 ||
889 (AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_64R6)
890 JrT9Instr = 0x03200009;
890891
891892 writeBytesUnaligned(LuiT9Instr, Addr, 4);
892 writeBytesUnaligned(AdduiT9Instr, Addr+4, 4);
893 writeBytesUnaligned(JrT9Instr, Addr+8, 4);
894 writeBytesUnaligned(NopInstr, Addr+12, 4);
893 writeBytesUnaligned(AdduiT9Instr, Addr + 4, 4);
894 writeBytesUnaligned(JrT9Instr, Addr + 8, 4);
895 writeBytesUnaligned(NopInstr, Addr + 12, 4);
896 return Addr;
897 } else if (IsMipsN64ABI) {
898 // 0: 3c190000 lui t9,%highest(addr).
899 // 4: 67390000 daddiu t9,t9,%higher(addr).
900 // 8: 0019CC38 dsll t9,t9,16.
901 // c: 67390000 daddiu t9,t9,%hi(addr).
902 // 10: 0019CC38 dsll t9,t9,16.
903 // 14: 67390000 daddiu t9,t9,%lo(addr).
904 // 18: 03200008 jr t9.
905 // 1c: 00000000 nop.
906 const unsigned LuiT9Instr = 0x3c190000, DaddiuT9Instr = 0x67390000,
907 DsllT9Instr = 0x19CC38;
908 const unsigned NopInstr = 0x0;
909 unsigned JrT9Instr = 0x03200008;
910 if ((AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_64R6)
911 JrT9Instr = 0x03200009;
912
913 writeBytesUnaligned(LuiT9Instr, Addr, 4);
914 writeBytesUnaligned(DaddiuT9Instr, Addr + 4, 4);
915 writeBytesUnaligned(DsllT9Instr, Addr + 8, 4);
916 writeBytesUnaligned(DaddiuT9Instr, Addr + 12, 4);
917 writeBytesUnaligned(DsllT9Instr, Addr + 16, 4);
918 writeBytesUnaligned(DaddiuT9Instr, Addr + 20, 4);
919 writeBytesUnaligned(JrT9Instr, Addr + 24, 4);
920 writeBytesUnaligned(NopInstr, Addr + 28, 4);
895921 return Addr;
896922 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
897923 // Depending on which version of the ELF ABI is in use, we need to
12751275 if (Value.SymbolName) {
12761276 addRelocationForSymbol(REHi, Value.SymbolName);
12771277 addRelocationForSymbol(RELo, Value.SymbolName);
1278 }
1279 else {
1278 } else {
12801279 addRelocationForSection(REHi, Value.SectionID);
12811280 addRelocationForSection(RELo, Value.SectionID);
12821281 }
13361335 RE.SymOffset = allocateGOTEntries(1);
13371336 GOTSymbolOffsets[TargetName] = RE.SymOffset;
13381337 }
1338 if (Value.SymbolName)
1339 addRelocationForSymbol(RE, Value.SymbolName);
1340 else
1341 addRelocationForSection(RE, Value.SectionID);
1342 } else if (RelType == ELF::R_MIPS_26) {
1343 // This is an Mips branch relocation, need to use a stub function.
1344 DEBUG(dbgs() << "\t\tThis is a Mips branch relocation.");
1345 SectionEntry &Section = Sections[SectionID];
1346
1347 // Look up for existing stub.
1348 StubMap::const_iterator i = Stubs.find(Value);
1349 if (i != Stubs.end()) {
1350 RelocationEntry RE(SectionID, Offset, RelType, i->second);
1351 addRelocationForSection(RE, SectionID);
1352 DEBUG(dbgs() << " Stub function found\n");
1353 } else {
1354 // Create a new stub function.
1355 DEBUG(dbgs() << " Create a new stub function\n");
1356 Stubs[Value] = Section.getStubOffset();
1357
1358 unsigned AbiVariant;
1359 O.getPlatformFlags(AbiVariant);
1360
1361 uint8_t *StubTargetAddr = createStubFunction(
1362 Section.getAddressWithOffset(Section.getStubOffset()), AbiVariant);
1363
1364 if (IsMipsN32ABI) {
1365 // Creating Hi and Lo relocations for the filled stub instructions.
1366 RelocationEntry REHi(SectionID, StubTargetAddr - Section.getAddress(),
1367 ELF::R_MIPS_HI16, Value.Addend);
1368 RelocationEntry RELo(SectionID,
1369 StubTargetAddr - Section.getAddress() + 4,
1370 ELF::R_MIPS_LO16, Value.Addend);
1371 if (Value.SymbolName) {
1372 addRelocationForSymbol(REHi, Value.SymbolName);
1373 addRelocationForSymbol(RELo, Value.SymbolName);
1374 } else {
1375 addRelocationForSection(REHi, Value.SectionID);
1376 addRelocationForSection(RELo, Value.SectionID);
1377 }
1378 } else {
1379 // Creating Highest, Higher, Hi and Lo relocations for the filled stub
1380 // instructions.
1381 RelocationEntry REHighest(SectionID,
1382 StubTargetAddr - Section.getAddress(),
1383 ELF::R_MIPS_HIGHEST, Value.Addend);
1384 RelocationEntry REHigher(SectionID,
1385 StubTargetAddr - Section.getAddress() + 4,
1386 ELF::R_MIPS_HIGHER, Value.Addend);
1387 RelocationEntry REHi(SectionID,
1388 StubTargetAddr - Section.getAddress() + 12,
1389 ELF::R_MIPS_HI16, Value.Addend);
1390 RelocationEntry RELo(SectionID,
1391 StubTargetAddr - Section.getAddress() + 20,
1392 ELF::R_MIPS_LO16, Value.Addend);
1393 if (Value.SymbolName) {
1394 addRelocationForSymbol(REHighest, Value.SymbolName);
1395 addRelocationForSymbol(REHigher, Value.SymbolName);
1396 addRelocationForSymbol(REHi, Value.SymbolName);
1397 addRelocationForSymbol(RELo, Value.SymbolName);
1398 } else {
1399 addRelocationForSection(REHighest, Value.SectionID);
1400 addRelocationForSection(REHigher, Value.SectionID);
1401 addRelocationForSection(REHi, Value.SectionID);
1402 addRelocationForSection(RELo, Value.SectionID);
1403 }
1404 }
1405 RelocationEntry RE(SectionID, Offset, RelType, Section.getStubOffset());
1406 addRelocationForSection(RE, SectionID);
1407 Section.advanceStubOffset(getMaxStubSize());
1408 }
1409 } else {
1410 processSimpleRelocation(SectionID, Offset, RelType, Value);
13391411 }
1340 if (Value.SymbolName)
1341 addRelocationForSymbol(RE, Value.SymbolName);
1342 else
1343 addRelocationForSection(RE, Value.SectionID);
1412
13441413 } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
13451414 if (RelType == ELF::R_PPC64_REL24) {
13461415 // Determine ABI variant in use for this object.
6565 return 20; // movz; movk; movk; movk; br
6666 if (Arch == Triple::arm || Arch == Triple::thumb)
6767 return 8; // 32-bit instruction and 32-bit address
68 else if (IsMipsO32ABI)
68 else if (IsMipsO32ABI || IsMipsN32ABI)
6969 return 16;
70 else if (IsMipsN64ABI)
71 return 32;
7072 else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le)
7173 return 44;
7274 else if (Arch == Triple::x86_64)
115115 << format("%llx", Section.getLoadAddressWithOffset(Offset))
116116 << " Value: 0x" << format("%llx", Value) << " Type: 0x"
117117 << format("%x", Type) << " Addend: 0x" << format("%llx", Addend)
118 << " Offset: " << format("%llx" PRIx64, Offset)
119 << " SID: " << format("%d", SectionID)
118120 << " SymOffset: " << format("%x", SymOffset) << "\n");
119121
120122 switch (Type) {
140142 return ((Value + Addend + 0x8000) >> 16) & 0xffff;
141143 case ELF::R_MIPS_LO16:
142144 return (Value + Addend) & 0xffff;
145 case ELF::R_MIPS_HIGHER:
146 return ((Value + Addend + 0x80008000) >> 32) & 0xffff;
147 case ELF::R_MIPS_HIGHEST:
148 return ((Value + Addend + 0x800080008000) >> 48) & 0xffff;
143149 case ELF::R_MIPS_CALL16:
144150 case ELF::R_MIPS_GOT_DISP:
145151 case ELF::R_MIPS_GOT_PAGE: {
214220 case ELF::R_MIPS_GPREL16:
215221 case ELF::R_MIPS_HI16:
216222 case ELF::R_MIPS_LO16:
223 case ELF::R_MIPS_HIGHER:
224 case ELF::R_MIPS_HIGHEST:
217225 case ELF::R_MIPS_PC16:
218226 case ELF::R_MIPS_PCHI16:
219227 case ELF::R_MIPS_PCLO16:
303311 << format("%p", Section.getLoadAddressWithOffset(Offset))
304312 << " Value: " << format("%x", Value)
305313 << " Type: " << format("%x", Type)
306 << " Addend: " << format("%x", Addend) << "\n");
314 << " Addend: " << format("%x", Addend)
315 << " SymOffset: " << format("%x", Offset) << "\n");
307316
308317 Value = evaluateMIPS32Relocation(Section, Offset, Value, Type);
309318
4343 sd $4, 8($fp)
4444
4545 # Test R_MIPS_26 relocation.
46 # rtdyld-check: decode_operand(insn1, 0)[27:0] = foo[27:0]
46 # rtdyld-check: decode_operand(insn1, 0)[27:0] = stub_addr(test_ELF_Mips64N64.o, .text, foo)[27:0]
4747 insn1:
4848 .option pic0
4949 jal foo
4343 sd $4, 8($fp)
4444
4545 # Test R_MIPS_26 relocation.
46 # rtdyld-check: decode_operand(insn1, 0)[27:0] = foo[27:0]
46 # rtdyld-check: decode_operand(insn1, 0)[27:0] = stub_addr(test_ELF_N32.o, .text, foo)[27:0]
4747 insn1:
4848 .option pic0
4949 jal foo