OLD | NEW |
1 //===- subzero/src/IceELFObjectWriter.cpp - ELF object file writer --------===// | 1 //===- subzero/src/IceELFObjectWriter.cpp - ELF object file writer --------===// |
2 // | 2 // |
3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 /// | 9 /// |
10 /// \file | 10 /// \file |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "IceInst.h" | 23 #include "IceInst.h" |
24 #include "IceOperand.h" | 24 #include "IceOperand.h" |
25 #include "llvm/Support/MathExtras.h" | 25 #include "llvm/Support/MathExtras.h" |
26 | 26 |
27 using namespace llvm::ELF; | 27 using namespace llvm::ELF; |
28 | 28 |
29 namespace Ice { | 29 namespace Ice { |
30 | 30 |
31 namespace { | 31 namespace { |
32 | 32 |
33 struct { | 33 constexpr struct { |
34 bool IsELF64; | 34 bool IsELF64; |
35 uint16_t ELFMachine; | 35 uint16_t ELFMachine; |
36 uint32_t ELFFlags; | 36 uint32_t ELFFlags; |
37 } ELFTargetInfo[] = { | 37 } ELFTargetInfo[TargetArch_NUM] = { |
38 #define X(tag, str, is_elf64, e_machine, e_flags) \ | 38 #define X(tag, str, is_elf64, e_machine, e_flags) \ |
39 { is_elf64, e_machine, e_flags } \ | 39 { is_elf64, e_machine, e_flags } \ |
40 , | 40 , |
41 TARGETARCH_TABLE | 41 TARGETARCH_TABLE |
42 #undef X | 42 #undef X |
43 }; | 43 }; |
44 | 44 |
45 bool isELF64(TargetArch Arch) { | 45 bool isELF64(const ClFlags &Flags) { |
46 if (Arch < TargetArch_NUM) | 46 const TargetArch Arch = Flags.getTargetArch(); |
47 return ELFTargetInfo[Arch].IsELF64; | 47 if (Arch >= TargetArch_NUM) { |
48 llvm_unreachable("Invalid target arch for isELF64"); | 48 llvm_unreachable("Invalid target arch for isELF64"); |
49 return false; | 49 return false; |
| 50 } |
| 51 |
| 52 if (!Flags.getUseSandboxing()) { |
| 53 // Unsandboxed code is always ELF32 (pexes are ILP32.) |
| 54 return false; |
| 55 } |
| 56 |
| 57 return ELFTargetInfo[Arch].IsELF64; |
50 } | 58 } |
51 | 59 |
52 uint16_t getELFMachine(TargetArch Arch) { | 60 uint16_t getELFMachine(TargetArch Arch) { |
53 if (Arch < TargetArch_NUM) | 61 if (Arch < TargetArch_NUM) |
54 return ELFTargetInfo[Arch].ELFMachine; | 62 return ELFTargetInfo[Arch].ELFMachine; |
55 llvm_unreachable("Invalid target arch for getELFMachine"); | 63 llvm_unreachable("Invalid target arch for getELFMachine"); |
56 return EM_NONE; | 64 return EM_NONE; |
57 } | 65 } |
58 | 66 |
59 uint32_t getELFFlags(TargetArch Arch) { | 67 uint32_t getELFFlags(TargetArch Arch) { |
60 if (Arch < TargetArch_NUM) | 68 if (Arch < TargetArch_NUM) |
61 return ELFTargetInfo[Arch].ELFFlags; | 69 return ELFTargetInfo[Arch].ELFFlags; |
62 llvm_unreachable("Invalid target arch for getELFFlags"); | 70 llvm_unreachable("Invalid target arch for getELFFlags"); |
63 return 0; | 71 return 0; |
64 } | 72 } |
65 | 73 |
66 } // end of anonymous namespace | 74 } // end of anonymous namespace |
67 | 75 |
68 ELFObjectWriter::ELFObjectWriter(GlobalContext &Ctx, ELFStreamer &Out) | 76 ELFObjectWriter::ELFObjectWriter(GlobalContext &Ctx, ELFStreamer &Out) |
69 : Ctx(Ctx), Str(Out), ELF64(isELF64(Ctx.getFlags().getTargetArch())) { | 77 : Ctx(Ctx), Str(Out), ELF64(isELF64(Ctx.getFlags())) { |
70 // Create the special bookkeeping sections now. | 78 // Create the special bookkeeping sections now. |
71 const IceString NullSectionName(""); | 79 const IceString NullSectionName(""); |
72 NullSection = new (Ctx.allocate<ELFSection>()) | 80 NullSection = new (Ctx.allocate<ELFSection>()) |
73 ELFSection(NullSectionName, SHT_NULL, 0, 0, 0); | 81 ELFSection(NullSectionName, SHT_NULL, 0, 0, 0); |
74 | 82 |
75 const IceString ShStrTabName(".shstrtab"); | 83 const IceString ShStrTabName(".shstrtab"); |
76 ShStrTab = new (Ctx.allocate<ELFStringTableSection>()) | 84 ShStrTab = new (Ctx.allocate<ELFStringTableSection>()) |
77 ELFStringTableSection(ShStrTabName, SHT_STRTAB, 0, 1, 0); | 85 ELFStringTableSection(ShStrTabName, SHT_STRTAB, 0, 1, 0); |
78 ShStrTab->add(ShStrTabName); | 86 ShStrTab->add(ShStrTabName); |
79 | 87 |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 ShAlign, 0); | 232 ShAlign, 0); |
225 Elf64_Off OffsetInFile = alignFileOffset(Section->getSectionAlign()); | 233 Elf64_Off OffsetInFile = alignFileOffset(Section->getSectionAlign()); |
226 Section->setFileOffset(OffsetInFile); | 234 Section->setFileOffset(OffsetInFile); |
227 TextSections.push_back(Section); | 235 TextSections.push_back(Section); |
228 RelSection = createRelocationSection(Section); | 236 RelSection = createRelocationSection(Section); |
229 RelTextSections.push_back(RelSection); | 237 RelTextSections.push_back(RelSection); |
230 } else { | 238 } else { |
231 Section = TextSections[0]; | 239 Section = TextSections[0]; |
232 RelSection = RelTextSections[0]; | 240 RelSection = RelTextSections[0]; |
233 } | 241 } |
234 RelocOffsetT OffsetInSection = Section->getCurrentSize(); | 242 const RelocOffsetT OffsetInSection = Section->getCurrentSize(); |
235 // Function symbols are set to 0 size in the symbol table, in contrast to | 243 // Function symbols are set to 0 size in the symbol table, in contrast to |
236 // data symbols which have a proper size. | 244 // data symbols which have a proper size. |
237 SizeT SymbolSize = 0; | 245 constexpr SizeT SymbolSize = 0; |
238 Section->appendData(Str, Asm->getBufferView()); | 246 Section->appendData(Str, Asm->getBufferView()); |
239 uint8_t SymbolType; | 247 uint8_t SymbolType; |
240 uint8_t SymbolBinding; | 248 uint8_t SymbolBinding; |
241 if (IsInternal && !Ctx.getFlags().getDisableInternal()) { | 249 if (IsInternal && !Ctx.getFlags().getDisableInternal()) { |
242 SymbolType = STT_NOTYPE; | 250 SymbolType = STT_NOTYPE; |
243 SymbolBinding = STB_LOCAL; | 251 SymbolBinding = STB_LOCAL; |
244 } else { | 252 } else { |
245 SymbolType = STT_FUNC; | 253 SymbolType = STT_FUNC; |
246 SymbolBinding = STB_GLOBAL; | 254 SymbolBinding = STB_GLOBAL; |
247 } | 255 } |
248 SymTab->createDefinedSym(FuncName, SymbolType, SymbolBinding, Section, | 256 SymTab->createDefinedSym(FuncName, SymbolType, SymbolBinding, Section, |
249 OffsetInSection, SymbolSize); | 257 OffsetInSection, SymbolSize); |
250 StrTab->add(FuncName); | 258 StrTab->add(FuncName); |
| 259 for (const auto &InternalReloc : Asm->getInternalRelocations()) { |
| 260 const IceString &RelocName = InternalReloc.first; |
| 261 constexpr uint8_t RelocSymbolType = STT_NOTYPE; |
| 262 constexpr uint8_t RelocSymbolBinding = STB_LOCAL; |
| 263 const SizeT RelocOffsetInSection = OffsetInSection + InternalReloc.second; |
| 264 SymTab->createDefinedSym(RelocName, RelocSymbolType, RelocSymbolBinding, |
| 265 Section, RelocOffsetInSection, SymbolSize); |
| 266 StrTab->add(RelocName); |
| 267 } |
251 | 268 |
252 // Copy the fixup information from per-function Assembler memory to the | 269 // Copy the fixup information from per-function Assembler memory to the |
253 // object writer's memory, for writing later. | 270 // object writer's memory, for writing later. |
254 if (!Asm->fixups().empty()) { | 271 if (!Asm->fixups().empty()) { |
255 RelSection->addRelocations(OffsetInSection, Asm->fixups()); | 272 RelSection->addRelocations(OffsetInSection, Asm->fixups()); |
256 } | 273 } |
257 } | 274 } |
258 | 275 |
259 namespace { | 276 namespace { |
260 | 277 |
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
672 if (ELF64) { | 689 if (ELF64) { |
673 writeELFHeaderInternal<true>(ShOffset, ShStrTab->getNumber(), | 690 writeELFHeaderInternal<true>(ShOffset, ShStrTab->getNumber(), |
674 AllSections.size()); | 691 AllSections.size()); |
675 } else { | 692 } else { |
676 writeELFHeaderInternal<false>(ShOffset, ShStrTab->getNumber(), | 693 writeELFHeaderInternal<false>(ShOffset, ShStrTab->getNumber(), |
677 AllSections.size()); | 694 AllSections.size()); |
678 } | 695 } |
679 } | 696 } |
680 | 697 |
681 } // end of namespace Ice | 698 } // end of namespace Ice |
OLD | NEW |