Use a default alignment for data and bss sections.

Only pad when the section size > 0 and move the code that deals
with globals initializers to a place we know for sure the global
is initialized.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73944 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Bruno Cardoso Lopes 2009-06-23 04:39:27 +00:00
parent bcd0b8d2ef
commit e39493eb1b
2 changed files with 25 additions and 19 deletions

View File

@ -159,10 +159,6 @@ ELFSection &ELFWriter::getGlobalSymELFSection(const GlobalVariable *GV,
unsigned Flags = S->getFlags(); unsigned Flags = S->getFlags();
unsigned SectionType = ELFSection::SHT_PROGBITS; unsigned SectionType = ELFSection::SHT_PROGBITS;
unsigned SHdrFlags = ELFSection::SHF_ALLOC; unsigned SHdrFlags = ELFSection::SHF_ALLOC;
const TargetData *TD = TM.getTargetData();
unsigned Align = TD->getPreferredAlignment(GV);
Constant *CV = GV->getInitializer();
DOUT << "Section " << S->getName() << " for global " << GV->getName() << "\n"; DOUT << "Section " << S->getName() << " for global " << GV->getName() << "\n";
// If this is an external global, the symbol does not have a section. // If this is an external global, the symbol does not have a section.
@ -171,6 +167,10 @@ ELFSection &ELFWriter::getGlobalSymELFSection(const GlobalVariable *GV,
return getNullSection(); return getNullSection();
} }
const TargetData *TD = TM.getTargetData();
unsigned Align = TD->getPreferredAlignment(GV);
Constant *CV = GV->getInitializer();
if (Flags & SectionFlags::Code) if (Flags & SectionFlags::Code)
SHdrFlags |= ELFSection::SHF_EXECINSTR; SHdrFlags |= ELFSection::SHF_EXECINSTR;
if (Flags & SectionFlags::Writeable) if (Flags & SectionFlags::Writeable)
@ -192,6 +192,7 @@ ELFSection &ELFWriter::getGlobalSymELFSection(const GlobalVariable *GV,
GV->hasCommonLinkage()) { GV->hasCommonLinkage()) {
Sym.SectionIdx = ELFSection::SHN_COMMON; Sym.SectionIdx = ELFSection::SHN_COMMON;
Sym.IsCommon = true; Sym.IsCommon = true;
ElfS.Align = 1;
return ElfS; return ElfS;
} }
Sym.IsBss = true; Sym.IsBss = true;
@ -218,19 +219,21 @@ void ELFWriter::EmitFunctionDeclaration(const Function *F) {
void ELFWriter::EmitGlobalVar(const GlobalVariable *GV) { void ELFWriter::EmitGlobalVar(const GlobalVariable *GV) {
unsigned SymBind = getGlobalELFLinkage(GV); unsigned SymBind = getGlobalELFLinkage(GV);
unsigned Align=0, Size=0;
ELFSym GblSym(GV); ELFSym GblSym(GV);
GblSym.setBind(SymBind); GblSym.setBind(SymBind);
if (GV->hasInitializer()) if (GV->hasInitializer()) {
GblSym.setType(ELFSym::STT_OBJECT); GblSym.setType(ELFSym::STT_OBJECT);
else const TargetData *TD = TM.getTargetData();
Align = TD->getPreferredAlignment(GV);
Size = TD->getTypeAllocSize(GV->getInitializer()->getType());
GblSym.Size = Size;
} else {
GblSym.setType(ELFSym::STT_NOTYPE); GblSym.setType(ELFSym::STT_NOTYPE);
}
ELFSection &GblSection = getGlobalSymELFSection(GV, GblSym); ELFSection &GblSection = getGlobalSymELFSection(GV, GblSym);
const TargetData *TD = TM.getTargetData();
unsigned Align = TD->getPreferredAlignment(GV);
unsigned Size = TD->getTypeAllocSize(GV->getInitializer()->getType());
GblSym.Size = Size;
if (GblSym.IsCommon) { if (GblSym.IsCommon) {
GblSym.Value = Align; GblSym.Value = Align;
@ -598,7 +601,7 @@ void ELFWriter::EmitSymbolTable() {
/// section names. /// section names.
void ELFWriter::EmitSectionTableStringTable() { void ELFWriter::EmitSectionTableStringTable() {
// First step: add the section for the string table to the list of sections: // First step: add the section for the string table to the list of sections:
ELFSection &SHStrTab = getSection(".shstrtab", ELFSection::SHT_STRTAB, 0); ELFSection &SHStrTab = getSectionHeaderStringTableSection();
// Now that we know which section number is the .shstrtab section, update the // Now that we know which section number is the .shstrtab section, update the
// e_shstrndx entry in the ELF header. // e_shstrndx entry in the ELF header.
@ -684,13 +687,12 @@ void ELFWriter::OutputSectionsAndSectionTable() {
<< ", SectionData Size: " << S.size() << "\n"; << ", SectionData Size: " << S.size() << "\n";
// Align FileOff to whatever the alignment restrictions of the section are. // Align FileOff to whatever the alignment restrictions of the section are.
if (S.Align) {
for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
FileOff != NewFileOff; ++FileOff)
O << (char)0xAB;
}
if (S.size()) { if (S.size()) {
if (S.Align) {
for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
FileOff != NewFileOff; ++FileOff)
O << (char)0xAB;
}
O.write((char *)&S.getData()[0], S.Size); O.write((char *)&S.getData()[0], S.Size);
FileOff += S.Size; FileOff += S.Size;
} }

View File

@ -171,14 +171,18 @@ namespace llvm {
return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1); return getSection(".strtab", ELFSection::SHT_STRTAB, 0, 1);
} }
ELFSection &getSectionHeaderStringTableSection() {
return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
}
ELFSection &getDataSection() { ELFSection &getDataSection() {
return getSection(".data", ELFSection::SHT_PROGBITS, return getSection(".data", ELFSection::SHT_PROGBITS,
ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC); ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
} }
ELFSection &getBSSSection() { ELFSection &getBSSSection() {
return getSection(".bss", ELFSection::SHT_NOBITS, return getSection(".bss", ELFSection::SHT_NOBITS,
ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC); ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
} }
ELFSection &getNullSection() { ELFSection &getNullSection() {