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 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 assert(Section < ELFObjectWriter::NumSectionTypes); | 278 assert(Section < ELFObjectWriter::NumSectionTypes); |
279 VarsBySection[Section].push_back(Var); | 279 VarsBySection[Section].push_back(Var); |
280 } | 280 } |
281 } | 281 } |
282 } | 282 } |
283 | 283 |
284 } // end of anonymous namespace | 284 } // end of anonymous namespace |
285 | 285 |
286 void ELFObjectWriter::writeDataSection(const VariableDeclarationList &Vars, | 286 void ELFObjectWriter::writeDataSection(const VariableDeclarationList &Vars, |
287 FixupKind RelocationKind, | 287 FixupKind RelocationKind, |
288 const IceString &SectionSuffix) { | 288 const IceString &SectionSuffix, |
| 289 bool IsPIC) { |
289 assert(!SectionNumbersAssigned); | 290 assert(!SectionNumbersAssigned); |
290 VariableDeclarationList VarsBySection[ELFObjectWriter::NumSectionTypes]; | 291 VariableDeclarationList VarsBySection[ELFObjectWriter::NumSectionTypes]; |
291 for (auto &SectionList : VarsBySection) | 292 for (auto &SectionList : VarsBySection) |
292 SectionList.reserve(Vars.size()); | 293 SectionList.reserve(Vars.size()); |
293 partitionGlobalsBySection(Vars, VarsBySection, | 294 partitionGlobalsBySection(Vars, VarsBySection, |
294 Ctx.getFlags().getTranslateOnly()); | 295 Ctx.getFlags().getTranslateOnly()); |
295 size_t I = 0; | 296 size_t I = 0; |
296 for (auto &SectionList : VarsBySection) { | 297 for (auto &SectionList : VarsBySection) { |
297 writeDataOfType(static_cast<SectionType>(I++), SectionList, RelocationKind, | 298 writeDataOfType(static_cast<SectionType>(I++), SectionList, RelocationKind, |
298 SectionSuffix); | 299 SectionSuffix, IsPIC); |
299 } | 300 } |
300 } | 301 } |
301 | 302 |
302 namespace { | 303 namespace { |
303 IceString MangleSectionName(const char Base[], const IceString &Suffix) { | 304 IceString MangleSectionName(const char Base[], const IceString &Suffix) { |
304 if (Suffix.empty()) | 305 if (Suffix.empty()) |
305 return Base; | 306 return Base; |
306 return Base + ("." + Suffix); | 307 return Base + ("." + Suffix); |
307 } | 308 } |
308 } // end of anonymous namespace | 309 } // end of anonymous namespace |
309 | 310 |
310 // TODO(jvoung): Handle fdata-sections. | 311 // TODO(jvoung): Handle fdata-sections. |
311 void ELFObjectWriter::writeDataOfType(SectionType ST, | 312 void ELFObjectWriter::writeDataOfType(SectionType ST, |
312 const VariableDeclarationList &Vars, | 313 const VariableDeclarationList &Vars, |
313 FixupKind RelocationKind, | 314 FixupKind RelocationKind, |
314 const IceString &SectionSuffix) { | 315 const IceString &SectionSuffix, |
| 316 bool IsPIC) { |
315 if (Vars.empty()) | 317 if (Vars.empty()) |
316 return; | 318 return; |
317 ELFDataSection *Section; | 319 ELFDataSection *Section; |
318 ELFRelocationSection *RelSection; | 320 ELFRelocationSection *RelSection; |
319 IceString SectionName; | 321 IceString SectionName; |
320 Elf64_Xword ShAddralign = 1; | 322 Elf64_Xword ShAddralign = 1; |
321 for (VariableDeclaration *Var : Vars) { | 323 for (VariableDeclaration *Var : Vars) { |
322 Elf64_Xword Align = Var->getAlignment(); | 324 Elf64_Xword Align = Var->getAlignment(); |
323 ShAddralign = std::max(ShAddralign, Align); | 325 ShAddralign = std::max(ShAddralign, Align); |
324 } | 326 } |
325 constexpr Elf64_Xword ShEntsize = 0; // non-uniform data element size. | 327 constexpr Elf64_Xword ShEntsize = 0; // non-uniform data element size. |
326 // Lift this out, so it can be re-used if we do fdata-sections? | 328 // Lift this out, so it can be re-used if we do fdata-sections? |
327 switch (ST) { | 329 switch (ST) { |
328 case ROData: { | 330 case ROData: { |
329 const IceString SectionName = MangleSectionName(".rodata", SectionSuffix); | 331 const IceString SectionName = |
330 constexpr Elf64_Xword ShFlags = SHF_ALLOC; | 332 MangleSectionName(IsPIC ? ".data.rel.ro" : ".rodata", SectionSuffix); |
| 333 const Elf64_Xword ShFlags = SHF_ALLOC | (IsPIC ? SHF_WRITE : 0); |
331 Section = createSection<ELFDataSection>(SectionName, SHT_PROGBITS, ShFlags, | 334 Section = createSection<ELFDataSection>(SectionName, SHT_PROGBITS, ShFlags, |
332 ShAddralign, ShEntsize); | 335 ShAddralign, ShEntsize); |
333 Section->setFileOffset(alignFileOffset(ShAddralign)); | 336 Section->setFileOffset(alignFileOffset(ShAddralign)); |
334 RODataSections.push_back(Section); | 337 RODataSections.push_back(Section); |
335 RelSection = createRelocationSection(Section); | 338 RelSection = createRelocationSection(Section); |
336 RelRODataSections.push_back(RelSection); | 339 RelRODataSections.push_back(RelSection); |
337 break; | 340 break; |
338 } | 341 } |
339 case Data: { | 342 case Data: { |
340 const IceString SectionName = MangleSectionName(".data", SectionSuffix); | 343 const IceString SectionName = MangleSectionName(".data", SectionSuffix); |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 | 553 |
551 template void ELFObjectWriter::writeConstantPool<ConstantInteger32>(Type Ty); | 554 template void ELFObjectWriter::writeConstantPool<ConstantInteger32>(Type Ty); |
552 | 555 |
553 void ELFObjectWriter::writeAllRelocationSections() { | 556 void ELFObjectWriter::writeAllRelocationSections() { |
554 writeRelocationSections(RelTextSections); | 557 writeRelocationSections(RelTextSections); |
555 writeRelocationSections(RelDataSections); | 558 writeRelocationSections(RelDataSections); |
556 writeRelocationSections(RelRODataSections); | 559 writeRelocationSections(RelRODataSections); |
557 } | 560 } |
558 | 561 |
559 void ELFObjectWriter::writeJumpTable(const JumpTableData &JT, | 562 void ELFObjectWriter::writeJumpTable(const JumpTableData &JT, |
560 FixupKind RelocationKind) { | 563 FixupKind RelocationKind, bool IsPIC) { |
561 ELFDataSection *Section; | 564 ELFDataSection *Section; |
562 ELFRelocationSection *RelSection; | 565 ELFRelocationSection *RelSection; |
563 const Elf64_Xword PointerSize = typeWidthInBytes(getPointerType()); | 566 const Elf64_Xword PointerSize = typeWidthInBytes(getPointerType()); |
564 const Elf64_Xword ShAddralign = PointerSize; | 567 const Elf64_Xword ShAddralign = PointerSize; |
565 const Elf64_Xword ShEntsize = PointerSize; | 568 const Elf64_Xword ShEntsize = PointerSize; |
566 const IceString SectionName = | 569 const IceString SectionName = MangleSectionName( |
567 MangleSectionName(".rodata", JT.getFunctionName() + "$jumptable"); | 570 IsPIC ? ".data.rel.ro" : ".rodata", JT.getFunctionName() + "$jumptable"); |
568 Section = createSection<ELFDataSection>(SectionName, SHT_PROGBITS, SHF_ALLOC, | 571 Section = createSection<ELFDataSection>(SectionName, SHT_PROGBITS, SHF_ALLOC, |
569 ShAddralign, ShEntsize); | 572 ShAddralign, ShEntsize); |
570 Section->setFileOffset(alignFileOffset(ShAddralign)); | 573 Section->setFileOffset(alignFileOffset(ShAddralign)); |
571 RODataSections.push_back(Section); | 574 RODataSections.push_back(Section); |
572 RelSection = createRelocationSection(Section); | 575 RelSection = createRelocationSection(Section); |
573 RelRODataSections.push_back(RelSection); | 576 RelRODataSections.push_back(RelSection); |
574 | 577 |
575 constexpr uint8_t SymbolType = STT_OBJECT; | 578 constexpr uint8_t SymbolType = STT_OBJECT; |
576 Section->padToAlignment(Str, PointerSize); | 579 Section->padToAlignment(Str, PointerSize); |
577 bool IsExternal = Ctx.getFlags().getDisableInternal(); | 580 bool IsExternal = Ctx.getFlags().getDisableInternal(); |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
669 if (ELF64) { | 672 if (ELF64) { |
670 writeELFHeaderInternal<true>(ShOffset, ShStrTab->getNumber(), | 673 writeELFHeaderInternal<true>(ShOffset, ShStrTab->getNumber(), |
671 AllSections.size()); | 674 AllSections.size()); |
672 } else { | 675 } else { |
673 writeELFHeaderInternal<false>(ShOffset, ShStrTab->getNumber(), | 676 writeELFHeaderInternal<false>(ShOffset, ShStrTab->getNumber(), |
674 AllSections.size()); | 677 AllSections.size()); |
675 } | 678 } |
676 } | 679 } |
677 | 680 |
678 } // end of namespace Ice | 681 } // end of namespace Ice |
OLD | NEW |