| 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. |
| (...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 size_t Section = classifyGlobalSection(Var); | 278 size_t Section = classifyGlobalSection(Var); |
| 279 assert(Section < ELFObjectWriter::NumSectionTypes); | 279 assert(Section < ELFObjectWriter::NumSectionTypes); |
| 280 VarsBySection[Section].push_back(Var); | 280 VarsBySection[Section].push_back(Var); |
| 281 } | 281 } |
| 282 } | 282 } |
| 283 } | 283 } |
| 284 | 284 |
| 285 } // end of anonymous namespace | 285 } // end of anonymous namespace |
| 286 | 286 |
| 287 void ELFObjectWriter::writeDataSection(const VariableDeclarationList &Vars, | 287 void ELFObjectWriter::writeDataSection(const VariableDeclarationList &Vars, |
| 288 FixupKind RelocationKind) { | 288 FixupKind RelocationKind, |
| 289 const IceString &SectionSuffix) { |
| 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, |
| 299 SectionSuffix); |
| 298 } | 300 } |
| 299 } | 301 } |
| 300 | 302 |
| 303 namespace { |
| 304 IceString MangleSectionName(const char Base[], const IceString &Suffix) { |
| 305 if (Suffix.empty()) |
| 306 return Base; |
| 307 return Base + ("." + Suffix); |
| 308 } |
| 309 } // end of anonymous namespace |
| 310 |
| 311 // TODO(jvoung): Handle fdata-sections. |
| 301 void ELFObjectWriter::writeDataOfType(SectionType ST, | 312 void ELFObjectWriter::writeDataOfType(SectionType ST, |
| 302 const VariableDeclarationList &Vars, | 313 const VariableDeclarationList &Vars, |
| 303 FixupKind RelocationKind) { | 314 FixupKind RelocationKind, |
| 315 const IceString &SectionSuffix) { |
| 304 if (Vars.empty()) | 316 if (Vars.empty()) |
| 305 return; | 317 return; |
| 306 ELFDataSection *Section; | 318 ELFDataSection *Section; |
| 307 ELFRelocationSection *RelSection; | 319 ELFRelocationSection *RelSection; |
| 308 // TODO(jvoung): Handle fdata-sections. | |
| 309 IceString SectionName; | 320 IceString SectionName; |
| 310 Elf64_Xword ShAddralign = 1; | 321 Elf64_Xword ShAddralign = 1; |
| 311 for (VariableDeclaration *Var : Vars) { | 322 for (VariableDeclaration *Var : Vars) { |
| 312 Elf64_Xword Align = Var->getAlignment(); | 323 Elf64_Xword Align = Var->getAlignment(); |
| 313 ShAddralign = std::max(ShAddralign, Align); | 324 ShAddralign = std::max(ShAddralign, Align); |
| 314 } | 325 } |
| 315 const Elf64_Xword ShEntsize = 0; // non-uniform data element size. | 326 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? | 327 // Lift this out, so it can be re-used if we do fdata-sections? |
| 317 switch (ST) { | 328 switch (ST) { |
| 318 case ROData: { | 329 case ROData: { |
| 319 SectionName = ".rodata"; | 330 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; | 331 const Elf64_Xword ShFlags = SHF_ALLOC; |
| 323 Section = createSection<ELFDataSection>(SectionName, SHT_PROGBITS, ShFlags, | 332 Section = createSection<ELFDataSection>(SectionName, SHT_PROGBITS, ShFlags, |
| 324 ShAddralign, ShEntsize); | 333 ShAddralign, ShEntsize); |
| 325 Section->setFileOffset(alignFileOffset(ShAddralign)); | 334 Section->setFileOffset(alignFileOffset(ShAddralign)); |
| 326 RODataSections.push_back(Section); | 335 RODataSections.push_back(Section); |
| 327 RelSection = createRelocationSection(Section); | 336 RelSection = createRelocationSection(Section); |
| 328 RelRODataSections.push_back(RelSection); | 337 RelRODataSections.push_back(RelSection); |
| 329 break; | 338 break; |
| 330 } | 339 } |
| 331 case Data: { | 340 case Data: { |
| 332 SectionName = ".data"; | 341 const IceString SectionName = MangleSectionName(".data", SectionSuffix); |
| 333 assert(DataSections.empty()); | |
| 334 const Elf64_Xword ShFlags = SHF_ALLOC | SHF_WRITE; | 342 const Elf64_Xword ShFlags = SHF_ALLOC | SHF_WRITE; |
| 335 Section = createSection<ELFDataSection>(SectionName, SHT_PROGBITS, ShFlags, | 343 Section = createSection<ELFDataSection>(SectionName, SHT_PROGBITS, ShFlags, |
| 336 ShAddralign, ShEntsize); | 344 ShAddralign, ShEntsize); |
| 337 Section->setFileOffset(alignFileOffset(ShAddralign)); | 345 Section->setFileOffset(alignFileOffset(ShAddralign)); |
| 338 DataSections.push_back(Section); | 346 DataSections.push_back(Section); |
| 339 RelSection = createRelocationSection(Section); | 347 RelSection = createRelocationSection(Section); |
| 340 RelDataSections.push_back(RelSection); | 348 RelDataSections.push_back(RelSection); |
| 341 break; | 349 break; |
| 342 } | 350 } |
| 343 case BSS: { | 351 case BSS: { |
| 344 SectionName = ".bss"; | 352 const IceString SectionName = MangleSectionName(".bss", SectionSuffix); |
| 345 assert(BSSSections.empty()); | |
| 346 const Elf64_Xword ShFlags = SHF_ALLOC | SHF_WRITE; | 353 const Elf64_Xword ShFlags = SHF_ALLOC | SHF_WRITE; |
| 347 Section = createSection<ELFDataSection>(SectionName, SHT_NOBITS, ShFlags, | 354 Section = createSection<ELFDataSection>(SectionName, SHT_NOBITS, ShFlags, |
| 348 ShAddralign, ShEntsize); | 355 ShAddralign, ShEntsize); |
| 349 Section->setFileOffset(alignFileOffset(ShAddralign)); | 356 Section->setFileOffset(alignFileOffset(ShAddralign)); |
| 350 BSSSections.push_back(Section); | 357 BSSSections.push_back(Section); |
| 351 break; | 358 break; |
| 352 } | 359 } |
| 353 case NumSectionTypes: | 360 case NumSectionTypes: |
| 354 llvm::report_fatal_error("Unknown SectionType"); | 361 llvm::report_fatal_error("Unknown SectionType"); |
| 355 break; | 362 break; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 376 assert(ST == BSS || ST == ROData); | 383 assert(ST == BSS || ST == ROData); |
| 377 if (ST == ROData) | 384 if (ST == ROData) |
| 378 Section->appendZeros(Str, SymbolSize); | 385 Section->appendZeros(Str, SymbolSize); |
| 379 else | 386 else |
| 380 Section->setSize(Section->getCurrentSize() + SymbolSize); | 387 Section->setSize(Section->getCurrentSize() + SymbolSize); |
| 381 } else { | 388 } else { |
| 382 assert(ST != BSS); | 389 assert(ST != BSS); |
| 383 for (VariableDeclaration::Initializer *Init : Var->getInitializers()) { | 390 for (VariableDeclaration::Initializer *Init : Var->getInitializers()) { |
| 384 switch (Init->getKind()) { | 391 switch (Init->getKind()) { |
| 385 case VariableDeclaration::Initializer::DataInitializerKind: { | 392 case VariableDeclaration::Initializer::DataInitializerKind: { |
| 386 const auto Data = | 393 const auto Data = llvm::cast<VariableDeclaration::DataInitializer>( |
| 387 llvm::cast<VariableDeclaration::DataInitializer>(Init) | 394 Init)->getContents(); |
| 388 ->getContents(); | |
| 389 Section->appendData(Str, llvm::StringRef(Data.data(), Data.size())); | 395 Section->appendData(Str, llvm::StringRef(Data.data(), Data.size())); |
| 390 break; | 396 break; |
| 391 } | 397 } |
| 392 case VariableDeclaration::Initializer::ZeroInitializerKind: | 398 case VariableDeclaration::Initializer::ZeroInitializerKind: |
| 393 Section->appendZeros(Str, Init->getNumBytes()); | 399 Section->appendZeros(Str, Init->getNumBytes()); |
| 394 break; | 400 break; |
| 395 case VariableDeclaration::Initializer::RelocInitializerKind: { | 401 case VariableDeclaration::Initializer::RelocInitializerKind: { |
| 396 const auto Reloc = | 402 const auto Reloc = |
| 397 llvm::cast<VariableDeclaration::RelocInitializer>(Init); | 403 llvm::cast<VariableDeclaration::RelocInitializer>(Init); |
| 398 AssemblerFixup NewFixup; | 404 AssemblerFixup NewFixup; |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 if (ELF64) { | 614 if (ELF64) { |
| 609 writeELFHeaderInternal<true>(ShOffset, ShStrTab->getNumber(), | 615 writeELFHeaderInternal<true>(ShOffset, ShStrTab->getNumber(), |
| 610 AllSections.size()); | 616 AllSections.size()); |
| 611 } else { | 617 } else { |
| 612 writeELFHeaderInternal<false>(ShOffset, ShStrTab->getNumber(), | 618 writeELFHeaderInternal<false>(ShOffset, ShStrTab->getNumber(), |
| 613 AllSections.size()); | 619 AllSections.size()); |
| 614 } | 620 } |
| 615 } | 621 } |
| 616 | 622 |
| 617 } // end of namespace Ice | 623 } // end of namespace Ice |
| OLD | NEW |