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