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 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 | 275 |
276 // Partition the Vars list by SectionType into VarsBySection. | 276 // Partition the Vars list by SectionType into VarsBySection. |
277 // If TranslateOnly is non-empty, then only the TranslateOnly variable | 277 // If TranslateOnly is non-empty, then only the TranslateOnly variable |
278 // is kept for emission. | 278 // is kept for emission. |
279 void partitionGlobalsBySection(const VariableDeclarationList &Vars, | 279 void partitionGlobalsBySection(const VariableDeclarationList &Vars, |
280 VariableDeclarationList VarsBySection[], | 280 VariableDeclarationList VarsBySection[], |
281 const IceString &TranslateOnly) { | 281 const IceString &TranslateOnly) { |
282 for (VariableDeclaration *Var : Vars) { | 282 for (VariableDeclaration *Var : Vars) { |
283 if (GlobalContext::matchSymbolName(Var->getName(), TranslateOnly)) { | 283 if (GlobalContext::matchSymbolName(Var->getName(), TranslateOnly)) { |
284 size_t Section = classifyGlobalSection(Var); | 284 size_t Section = classifyGlobalSection(Var); |
285 assert(Section < ELFObjectWriter::SectionType::NumSectionTypes); | 285 assert(Section < ELFObjectWriter::NumSectionTypes); |
286 VarsBySection[Section].push_back(Var); | 286 VarsBySection[Section].push_back(Var); |
287 } | 287 } |
288 } | 288 } |
289 } | 289 } |
290 | 290 |
291 } // end of anonymous namespace | 291 } // end of anonymous namespace |
292 | 292 |
293 void ELFObjectWriter::writeDataSection(const VariableDeclarationList &Vars, | 293 void ELFObjectWriter::writeDataSection(const VariableDeclarationList &Vars, |
294 FixupKind RelocationKind) { | 294 FixupKind RelocationKind) { |
295 assert(!SectionNumbersAssigned); | 295 assert(!SectionNumbersAssigned); |
296 VariableDeclarationList VarsBySection[ELFObjectWriter::NumSectionTypes]; | 296 VariableDeclarationList VarsBySection[ELFObjectWriter::NumSectionTypes]; |
297 for (auto &SectionList : VarsBySection) | 297 for (auto &SectionList : VarsBySection) |
298 SectionList.reserve(Vars.size()); | 298 SectionList.reserve(Vars.size()); |
299 partitionGlobalsBySection(Vars, VarsBySection, Ctx.getFlags().TranslateOnly); | 299 partitionGlobalsBySection(Vars, VarsBySection, Ctx.getFlags().TranslateOnly); |
300 bool IsELF64 = isELF64(Ctx.getTargetArch()); | 300 bool IsELF64 = isELF64(Ctx.getTargetArch()); |
301 size_t I = 0; | 301 size_t I = 0; |
302 for (auto &SectionList : VarsBySection) { | 302 for (auto &SectionList : VarsBySection) { |
303 writeDataOfType(static_cast<SectionType>(I++), SectionList, RelocationKind, | 303 writeDataOfType(static_cast<SectionType>(I++), SectionList, RelocationKind, |
304 IsELF64); | 304 IsELF64); |
305 } | 305 } |
306 } | 306 } |
307 | 307 |
308 void ELFObjectWriter::writeDataOfType(SectionType SectionType, | 308 void ELFObjectWriter::writeDataOfType(SectionType ST, |
309 const VariableDeclarationList &Vars, | 309 const VariableDeclarationList &Vars, |
310 FixupKind RelocationKind, bool IsELF64) { | 310 FixupKind RelocationKind, bool IsELF64) { |
311 ELFDataSection *Section; | 311 ELFDataSection *Section; |
312 ELFRelocationSection *RelSection; | 312 ELFRelocationSection *RelSection; |
313 // TODO(jvoung): Handle fdata-sections. | 313 // TODO(jvoung): Handle fdata-sections. |
314 IceString SectionName; | 314 IceString SectionName; |
315 Elf64_Xword ShAddralign = 0; | 315 Elf64_Xword ShAddralign = 0; |
316 for (VariableDeclaration *Var : Vars) { | 316 for (VariableDeclaration *Var : Vars) { |
317 Elf64_Xword Align = Var->getAlignment(); | 317 Elf64_Xword Align = Var->getAlignment(); |
318 ShAddralign = std::max(ShAddralign, Align); | 318 ShAddralign = std::max(ShAddralign, Align); |
319 } | 319 } |
320 const Elf64_Xword ShEntsize = 0; // non-uniform data element size. | 320 const Elf64_Xword ShEntsize = 0; // non-uniform data element size. |
321 // Lift this out, so it can be re-used if we do fdata-sections? | 321 // Lift this out, so it can be re-used if we do fdata-sections? |
322 switch (SectionType) { | 322 switch (ST) { |
323 case SectionType::ROData: { | 323 case ROData: { |
324 SectionName = ".rodata"; | 324 SectionName = ".rodata"; |
325 // Only expecting to write the data sections all in one shot for now. | 325 // Only expecting to write the data sections all in one shot for now. |
326 assert(RODataSections.empty()); | 326 assert(RODataSections.empty()); |
327 const Elf64_Xword ShFlags = SHF_ALLOC; | 327 const Elf64_Xword ShFlags = SHF_ALLOC; |
328 Section = createSection<ELFDataSection>(SectionName, SHT_PROGBITS, ShFlags, | 328 Section = createSection<ELFDataSection>(SectionName, SHT_PROGBITS, ShFlags, |
329 ShAddralign, ShEntsize); | 329 ShAddralign, ShEntsize); |
330 Section->setFileOffset(alignFileOffset(ShAddralign)); | 330 Section->setFileOffset(alignFileOffset(ShAddralign)); |
331 RODataSections.push_back(Section); | 331 RODataSections.push_back(Section); |
332 RelSection = createRelocationSection(IsELF64, Section); | 332 RelSection = createRelocationSection(IsELF64, Section); |
333 RelRODataSections.push_back(RelSection); | 333 RelRODataSections.push_back(RelSection); |
334 break; | 334 break; |
335 } | 335 } |
336 case SectionType::Data: { | 336 case Data: { |
337 SectionName = ".data"; | 337 SectionName = ".data"; |
338 assert(DataSections.empty()); | 338 assert(DataSections.empty()); |
339 const Elf64_Xword ShFlags = SHF_ALLOC | SHF_WRITE; | 339 const Elf64_Xword ShFlags = SHF_ALLOC | SHF_WRITE; |
340 Section = createSection<ELFDataSection>(SectionName, SHT_PROGBITS, ShFlags, | 340 Section = createSection<ELFDataSection>(SectionName, SHT_PROGBITS, ShFlags, |
341 ShAddralign, ShEntsize); | 341 ShAddralign, ShEntsize); |
342 Section->setFileOffset(alignFileOffset(ShAddralign)); | 342 Section->setFileOffset(alignFileOffset(ShAddralign)); |
343 DataSections.push_back(Section); | 343 DataSections.push_back(Section); |
344 RelSection = createRelocationSection(IsELF64, Section); | 344 RelSection = createRelocationSection(IsELF64, Section); |
345 RelDataSections.push_back(RelSection); | 345 RelDataSections.push_back(RelSection); |
346 break; | 346 break; |
347 } | 347 } |
348 case SectionType::BSS: { | 348 case BSS: { |
349 SectionName = ".bss"; | 349 SectionName = ".bss"; |
350 assert(BSSSections.empty()); | 350 assert(BSSSections.empty()); |
351 const Elf64_Xword ShFlags = SHF_ALLOC | SHF_WRITE; | 351 const Elf64_Xword ShFlags = SHF_ALLOC | SHF_WRITE; |
352 Section = createSection<ELFDataSection>(SectionName, SHT_NOBITS, ShFlags, | 352 Section = createSection<ELFDataSection>(SectionName, SHT_NOBITS, ShFlags, |
353 ShAddralign, ShEntsize); | 353 ShAddralign, ShEntsize); |
354 Section->setFileOffset(alignFileOffset(ShAddralign)); | 354 Section->setFileOffset(alignFileOffset(ShAddralign)); |
355 BSSSections.push_back(Section); | 355 BSSSections.push_back(Section); |
356 break; | 356 break; |
357 } | 357 } |
358 case SectionType::NumSectionTypes: | 358 case NumSectionTypes: |
359 llvm::report_fatal_error("Unknown SectionType"); | 359 llvm::report_fatal_error("Unknown SectionType"); |
360 break; | 360 break; |
361 } | 361 } |
362 | 362 |
363 const uint8_t SymbolType = STT_OBJECT; | 363 const uint8_t SymbolType = STT_OBJECT; |
364 for (VariableDeclaration *Var : Vars) { | 364 for (VariableDeclaration *Var : Vars) { |
365 Elf64_Xword Align = Var->getAlignment(); | 365 Elf64_Xword Align = Var->getAlignment(); |
366 Section->padToAlignment(Str, Align); | 366 Section->padToAlignment(Str, Align); |
367 SizeT SymbolSize = Var->getNumBytes(); | 367 SizeT SymbolSize = Var->getNumBytes(); |
368 bool IsExternal = Var->isExternal() || Ctx.getFlags().DisableInternal; | 368 bool IsExternal = Var->isExternal() || Ctx.getFlags().DisableInternal; |
369 const uint8_t SymbolBinding = IsExternal ? STB_GLOBAL : STB_LOCAL; | 369 const uint8_t SymbolBinding = IsExternal ? STB_GLOBAL : STB_LOCAL; |
370 IceString MangledName = Var->mangleName(&Ctx); | 370 IceString MangledName = Var->mangleName(&Ctx); |
371 SymTab->createDefinedSym(MangledName, SymbolType, SymbolBinding, Section, | 371 SymTab->createDefinedSym(MangledName, SymbolType, SymbolBinding, Section, |
372 Section->getCurrentSize(), SymbolSize); | 372 Section->getCurrentSize(), SymbolSize); |
373 StrTab->add(MangledName); | 373 StrTab->add(MangledName); |
374 if (!Var->hasNonzeroInitializer()) { | 374 if (!Var->hasNonzeroInitializer()) { |
375 assert(SectionType == SectionType::BSS || | 375 assert(ST == BSS || ST == ROData); |
376 SectionType == SectionType::ROData); | 376 if (ST == ROData) |
377 if (SectionType == SectionType::ROData) | |
378 Section->appendZeros(Str, SymbolSize); | 377 Section->appendZeros(Str, SymbolSize); |
379 else | 378 else |
380 Section->setSize(Section->getCurrentSize() + SymbolSize); | 379 Section->setSize(Section->getCurrentSize() + SymbolSize); |
381 } else { | 380 } else { |
382 assert(SectionType != SectionType::BSS); | 381 assert(ST != BSS); |
383 for (VariableDeclaration::Initializer *Init : Var->getInitializers()) { | 382 for (VariableDeclaration::Initializer *Init : Var->getInitializers()) { |
384 switch (Init->getKind()) { | 383 switch (Init->getKind()) { |
385 case VariableDeclaration::Initializer::DataInitializerKind: { | 384 case VariableDeclaration::Initializer::DataInitializerKind: { |
386 const auto Data = llvm::cast<VariableDeclaration::DataInitializer>( | 385 const auto Data = llvm::cast<VariableDeclaration::DataInitializer>( |
387 Init)->getContents(); | 386 Init)->getContents(); |
388 Section->appendData(Str, llvm::StringRef(Data.data(), Data.size())); | 387 Section->appendData(Str, llvm::StringRef(Data.data(), Data.size())); |
389 break; | 388 break; |
390 } | 389 } |
391 case VariableDeclaration::Initializer::ZeroInitializerKind: | 390 case VariableDeclaration::Initializer::ZeroInitializerKind: |
392 Section->appendZeros(Str, Init->getNumBytes()); | 391 Section->appendZeros(Str, Init->getNumBytes()); |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
592 if (IsELF64) { | 591 if (IsELF64) { |
593 writeELFHeaderInternal<true>(ShOffset, ShStrTab->getNumber(), | 592 writeELFHeaderInternal<true>(ShOffset, ShStrTab->getNumber(), |
594 AllSections.size()); | 593 AllSections.size()); |
595 } else { | 594 } else { |
596 writeELFHeaderInternal<false>(ShOffset, ShStrTab->getNumber(), | 595 writeELFHeaderInternal<false>(ShOffset, ShStrTab->getNumber(), |
597 AllSections.size()); | 596 AllSections.size()); |
598 } | 597 } |
599 } | 598 } |
600 | 599 |
601 } // end of namespace Ice | 600 } // end of namespace Ice |
OLD | NEW |