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