| 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 // This file defines the writer for ELF relocatable object files. | 10 // This file defines the writer for ELF relocatable object files. |
| 11 // | 11 // |
| 12 //===----------------------------------------------------------------------===// | 12 //===----------------------------------------------------------------------===// |
| 13 | 13 |
| 14 #include <sstream> |
| 15 |
| 14 #include "llvm/Support/MathExtras.h" | 16 #include "llvm/Support/MathExtras.h" |
| 15 | 17 |
| 16 #include "IceAssembler.h" | 18 #include "IceAssembler.h" |
| 17 #include "IceDefs.h" | 19 #include "IceDefs.h" |
| 18 #include "IceELFObjectWriter.h" | 20 #include "IceELFObjectWriter.h" |
| 19 #include "IceELFSection.h" | 21 #include "IceELFSection.h" |
| 20 #include "IceELFStreamer.h" | 22 #include "IceELFStreamer.h" |
| 21 #include "IceGlobalContext.h" | 23 #include "IceGlobalContext.h" |
| 22 #include "IceGlobalInits.h" | 24 #include "IceGlobalInits.h" |
| 23 #include "IceOperand.h" | 25 #include "IceOperand.h" |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 size_t Section = classifyGlobalSection(Var); | 280 size_t Section = classifyGlobalSection(Var); |
| 279 assert(Section < ELFObjectWriter::NumSectionTypes); | 281 assert(Section < ELFObjectWriter::NumSectionTypes); |
| 280 VarsBySection[Section].push_back(Var); | 282 VarsBySection[Section].push_back(Var); |
| 281 } | 283 } |
| 282 } | 284 } |
| 283 } | 285 } |
| 284 | 286 |
| 285 } // end of anonymous namespace | 287 } // end of anonymous namespace |
| 286 | 288 |
| 287 void ELFObjectWriter::writeDataSection(const VariableDeclarationList &Vars, | 289 void ELFObjectWriter::writeDataSection(const VariableDeclarationList &Vars, |
| 288 FixupKind RelocationKind) { | 290 FixupKind RelocationKind, |
| 291 const IceString &SectionSuffix) { |
| 289 assert(!SectionNumbersAssigned); | 292 assert(!SectionNumbersAssigned); |
| 290 VariableDeclarationList VarsBySection[ELFObjectWriter::NumSectionTypes]; | 293 VariableDeclarationList VarsBySection[ELFObjectWriter::NumSectionTypes]; |
| 291 for (auto &SectionList : VarsBySection) | 294 for (auto &SectionList : VarsBySection) |
| 292 SectionList.reserve(Vars.size()); | 295 SectionList.reserve(Vars.size()); |
| 293 partitionGlobalsBySection(Vars, VarsBySection, | 296 partitionGlobalsBySection(Vars, VarsBySection, |
| 294 Ctx.getFlags().getTranslateOnly()); | 297 Ctx.getFlags().getTranslateOnly()); |
| 295 size_t I = 0; | 298 size_t I = 0; |
| 296 for (auto &SectionList : VarsBySection) { | 299 for (auto &SectionList : VarsBySection) { |
| 297 writeDataOfType(static_cast<SectionType>(I++), SectionList, RelocationKind); | 300 writeDataOfType(static_cast<SectionType>(I++), SectionList, RelocationKind, |
| 301 SectionSuffix); |
| 298 } | 302 } |
| 299 } | 303 } |
| 300 | 304 |
| 305 namespace { |
| 306 IceString MangleSectionName(const char Base[], const IceString &Suffix) { |
| 307 if (Suffix.empty()) |
| 308 return Base; |
| 309 return (std::ostringstream() << Base << "." << Suffix).str(); |
| 310 } |
| 311 } // end of anonymous namespace |
| 312 |
| 313 // TODO(jvoung): Handle fdata-sections. |
| 301 void ELFObjectWriter::writeDataOfType(SectionType ST, | 314 void ELFObjectWriter::writeDataOfType(SectionType ST, |
| 302 const VariableDeclarationList &Vars, | 315 const VariableDeclarationList &Vars, |
| 303 FixupKind RelocationKind) { | 316 FixupKind RelocationKind, |
| 317 const IceString &SectionSuffix) { |
| 304 if (Vars.empty()) | 318 if (Vars.empty()) |
| 305 return; | 319 return; |
| 306 ELFDataSection *Section; | 320 ELFDataSection *Section; |
| 307 ELFRelocationSection *RelSection; | 321 ELFRelocationSection *RelSection; |
| 308 // TODO(jvoung): Handle fdata-sections. | |
| 309 IceString SectionName; | 322 IceString SectionName; |
| 310 Elf64_Xword ShAddralign = 1; | 323 Elf64_Xword ShAddralign = 1; |
| 311 for (VariableDeclaration *Var : Vars) { | 324 for (VariableDeclaration *Var : Vars) { |
| 312 Elf64_Xword Align = Var->getAlignment(); | 325 Elf64_Xword Align = Var->getAlignment(); |
| 313 ShAddralign = std::max(ShAddralign, Align); | 326 ShAddralign = std::max(ShAddralign, Align); |
| 314 } | 327 } |
| 315 const Elf64_Xword ShEntsize = 0; // non-uniform data element size. | 328 const Elf64_Xword ShEntsize = 0; // non-uniform data element size. |
| 316 // Lift this out, so it can be re-used if we do fdata-sections? | 329 // Lift this out, so it can be re-used if we do fdata-sections? |
| 317 switch (ST) { | 330 switch (ST) { |
| 318 case ROData: { | 331 case ROData: { |
| 319 SectionName = ".rodata"; | 332 const IceString SectionName = MangleSectionName(".rodata", SectionSuffix); |
| 320 // Only expecting to write the data sections all in one shot for now. | |
| 321 assert(RODataSections.empty()); | |
| 322 const Elf64_Xword ShFlags = SHF_ALLOC; | 333 const Elf64_Xword ShFlags = SHF_ALLOC; |
| 323 Section = createSection<ELFDataSection>(SectionName, SHT_PROGBITS, ShFlags, | 334 Section = createSection<ELFDataSection>(SectionName, SHT_PROGBITS, ShFlags, |
| 324 ShAddralign, ShEntsize); | 335 ShAddralign, ShEntsize); |
| 325 Section->setFileOffset(alignFileOffset(ShAddralign)); | 336 Section->setFileOffset(alignFileOffset(ShAddralign)); |
| 326 RODataSections.push_back(Section); | 337 RODataSections.push_back(Section); |
| 327 RelSection = createRelocationSection(Section); | 338 RelSection = createRelocationSection(Section); |
| 328 RelRODataSections.push_back(RelSection); | 339 RelRODataSections.push_back(RelSection); |
| 329 break; | 340 break; |
| 330 } | 341 } |
| 331 case Data: { | 342 case Data: { |
| 332 SectionName = ".data"; | 343 const IceString SectionName = MangleSectionName(".data", SectionSuffix); |
| 333 assert(DataSections.empty()); | |
| 334 const Elf64_Xword ShFlags = SHF_ALLOC | SHF_WRITE; | 344 const Elf64_Xword ShFlags = SHF_ALLOC | SHF_WRITE; |
| 335 Section = createSection<ELFDataSection>(SectionName, SHT_PROGBITS, ShFlags, | 345 Section = createSection<ELFDataSection>(SectionName, SHT_PROGBITS, ShFlags, |
| 336 ShAddralign, ShEntsize); | 346 ShAddralign, ShEntsize); |
| 337 Section->setFileOffset(alignFileOffset(ShAddralign)); | 347 Section->setFileOffset(alignFileOffset(ShAddralign)); |
| 338 DataSections.push_back(Section); | 348 DataSections.push_back(Section); |
| 339 RelSection = createRelocationSection(Section); | 349 RelSection = createRelocationSection(Section); |
| 340 RelDataSections.push_back(RelSection); | 350 RelDataSections.push_back(RelSection); |
| 341 break; | 351 break; |
| 342 } | 352 } |
| 343 case BSS: { | 353 case BSS: { |
| 344 SectionName = ".bss"; | 354 const IceString SectionName = MangleSectionName(".bss", SectionSuffix); |
| 345 assert(BSSSections.empty()); | |
| 346 const Elf64_Xword ShFlags = SHF_ALLOC | SHF_WRITE; | 355 const Elf64_Xword ShFlags = SHF_ALLOC | SHF_WRITE; |
| 347 Section = createSection<ELFDataSection>(SectionName, SHT_NOBITS, ShFlags, | 356 Section = createSection<ELFDataSection>(SectionName, SHT_NOBITS, ShFlags, |
| 348 ShAddralign, ShEntsize); | 357 ShAddralign, ShEntsize); |
| 349 Section->setFileOffset(alignFileOffset(ShAddralign)); | 358 Section->setFileOffset(alignFileOffset(ShAddralign)); |
| 350 BSSSections.push_back(Section); | 359 BSSSections.push_back(Section); |
| 351 break; | 360 break; |
| 352 } | 361 } |
| 353 case NumSectionTypes: | 362 case NumSectionTypes: |
| 354 llvm::report_fatal_error("Unknown SectionType"); | 363 llvm::report_fatal_error("Unknown SectionType"); |
| 355 break; | 364 break; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 376 assert(ST == BSS || ST == ROData); | 385 assert(ST == BSS || ST == ROData); |
| 377 if (ST == ROData) | 386 if (ST == ROData) |
| 378 Section->appendZeros(Str, SymbolSize); | 387 Section->appendZeros(Str, SymbolSize); |
| 379 else | 388 else |
| 380 Section->setSize(Section->getCurrentSize() + SymbolSize); | 389 Section->setSize(Section->getCurrentSize() + SymbolSize); |
| 381 } else { | 390 } else { |
| 382 assert(ST != BSS); | 391 assert(ST != BSS); |
| 383 for (VariableDeclaration::Initializer *Init : Var->getInitializers()) { | 392 for (VariableDeclaration::Initializer *Init : Var->getInitializers()) { |
| 384 switch (Init->getKind()) { | 393 switch (Init->getKind()) { |
| 385 case VariableDeclaration::Initializer::DataInitializerKind: { | 394 case VariableDeclaration::Initializer::DataInitializerKind: { |
| 386 const auto Data = | 395 const auto Data = llvm::cast<VariableDeclaration::DataInitializer>( |
| 387 llvm::cast<VariableDeclaration::DataInitializer>(Init) | 396 Init)->getContents(); |
| 388 ->getContents(); | |
| 389 Section->appendData(Str, llvm::StringRef(Data.data(), Data.size())); | 397 Section->appendData(Str, llvm::StringRef(Data.data(), Data.size())); |
| 390 break; | 398 break; |
| 391 } | 399 } |
| 392 case VariableDeclaration::Initializer::ZeroInitializerKind: | 400 case VariableDeclaration::Initializer::ZeroInitializerKind: |
| 393 Section->appendZeros(Str, Init->getNumBytes()); | 401 Section->appendZeros(Str, Init->getNumBytes()); |
| 394 break; | 402 break; |
| 395 case VariableDeclaration::Initializer::RelocInitializerKind: { | 403 case VariableDeclaration::Initializer::RelocInitializerKind: { |
| 396 const auto Reloc = | 404 const auto Reloc = |
| 397 llvm::cast<VariableDeclaration::RelocInitializer>(Init); | 405 llvm::cast<VariableDeclaration::RelocInitializer>(Init); |
| 398 AssemblerFixup NewFixup; | 406 AssemblerFixup NewFixup; |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 if (ELF64) { | 616 if (ELF64) { |
| 609 writeELFHeaderInternal<true>(ShOffset, ShStrTab->getNumber(), | 617 writeELFHeaderInternal<true>(ShOffset, ShStrTab->getNumber(), |
| 610 AllSections.size()); | 618 AllSections.size()); |
| 611 } else { | 619 } else { |
| 612 writeELFHeaderInternal<false>(ShOffset, ShStrTab->getNumber(), | 620 writeELFHeaderInternal<false>(ShOffset, ShStrTab->getNumber(), |
| 613 AllSections.size()); | 621 AllSections.size()); |
| 614 } | 622 } |
| 615 } | 623 } |
| 616 | 624 |
| 617 } // end of namespace Ice | 625 } // end of namespace Ice |
| OLD | NEW |