Remove hack used to strip unwanted chars from section name

Use MCSectionELF methods as much as possible, removing some
ELFWriter methods which are now unused



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78940 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Bruno Cardoso Lopes
2009-08-13 21:25:27 +00:00
parent b8e1055f41
commit d163e8b14c
4 changed files with 88 additions and 147 deletions

View File

@@ -74,19 +74,19 @@ namespace llvm {
}; };
unsigned SourceType; unsigned SourceType;
bool isGlobalValue() { return SourceType == isGV; } bool isGlobalValue() const { return SourceType == isGV; }
bool isExternalSym() { return SourceType == isExtSym; } bool isExternalSym() const { return SourceType == isExtSym; }
// getGlobalValue - If this is a global value which originated the // getGlobalValue - If this is a global value which originated the
// elf symbol, return a reference to it. // elf symbol, return a reference to it.
const GlobalValue *getGlobalValue() { const GlobalValue *getGlobalValue() const {
assert(SourceType == isGV && "This is not a global value"); assert(SourceType == isGV && "This is not a global value");
return Source.GV; return Source.GV;
}; };
// getExternalSym - If this is an external symbol which originated the // getExternalSym - If this is an external symbol which originated the
// elf symbol, return a reference to it. // elf symbol, return a reference to it.
const char *getExternalSymbol() { const char *getExternalSymbol() const {
assert(SourceType == isExtSym && "This is not an external symbol"); assert(SourceType == isExtSym && "This is not an external symbol");
return Source.Ext; return Source.Ext;
}; };

View File

@@ -73,7 +73,7 @@ bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
EW.getGlobalELFVisibility(F)); EW.getGlobalELFVisibility(F));
FnSym->SectionIdx = ES->SectionIdx; FnSym->SectionIdx = ES->SectionIdx;
FnSym->Size = ES->getCurrentPCOffset()-FnStartOff; FnSym->Size = ES->getCurrentPCOffset()-FnStartOff;
EW.addGlobalSymbol(F, true); EW.AddPendingGlobalSymbol(F, true);
// Offset from start of Section // Offset from start of Section
FnSym->Value = FnStartOff; FnSym->Value = FnStartOff;
@@ -102,9 +102,9 @@ bool ELFCodeEmitter::finishFunction(MachineFunction &MF) {
MachineRelocation &MR = Relocations[i]; MachineRelocation &MR = Relocations[i];
intptr_t Addr; intptr_t Addr;
if (MR.isGlobalValue()) { if (MR.isGlobalValue()) {
EW.addGlobalSymbol(MR.getGlobalValue()); EW.AddPendingGlobalSymbol(MR.getGlobalValue());
} else if (MR.isExternalSymbol()) { } else if (MR.isExternalSymbol()) {
EW.addExternalSymbol(MR.getExternalSymbol()); EW.AddPendingExternalSymbol(MR.getExternalSymbol());
} else if (MR.isBasicBlock()) { } else if (MR.isBasicBlock()) {
Addr = getMachineBasicBlockAddress(MR.getBasicBlock()); Addr = getMachineBasicBlockAddress(MR.getBasicBlock());
MR.setConstantVal(ES->SectionIdx); MR.setConstantVal(ES->SectionIdx);

View File

@@ -159,43 +159,52 @@ bool ELFWriter::doInitialization(Module &M) {
return false; return false;
} }
// addGlobalSymbol - Add a global to be processed and to the global symbol // AddPendingGlobalSymbol - Add a global to be processed and to
// lookup, use a zero index because the table index will be determined later. // the global symbol lookup, use a zero index because the table
void ELFWriter::addGlobalSymbol(const GlobalValue *GV, // index will be determined later.
bool AddToLookup /* = false */) { void ELFWriter::AddPendingGlobalSymbol(const GlobalValue *GV,
bool AddToLookup /* = false */) {
PendingGlobals.insert(GV); PendingGlobals.insert(GV);
if (AddToLookup) if (AddToLookup)
GblSymLookup[GV] = 0; GblSymLookup[GV] = 0;
} }
// addExternalSymbol - Add the external to be processed and to the // AddPendingExternalSymbol - Add the external to be processed
// external symbol lookup, use a zero index because the symbol // and to the external symbol lookup, use a zero index because
// table index will be determined later // the symbol table index will be determined later.
void ELFWriter::addExternalSymbol(const char *External) { void ELFWriter::AddPendingExternalSymbol(const char *External) {
PendingExternals.insert(External); PendingExternals.insert(External);
ExtSymLookup[External] = 0; ExtSymLookup[External] = 0;
} }
ELFSection &ELFWriter::getDataSection() {
const MCSectionELF *Data = (const MCSectionELF *)TLOF.getDataSection();
return getSection(Data->getSectionName(), Data->getType(),
Data->getFlags(), 4);
}
ELFSection &ELFWriter::getBSSSection() {
const MCSectionELF *BSS = (const MCSectionELF *)TLOF.getBSSSection();
return getSection(BSS->getSectionName(), BSS->getType(), BSS->getFlags(), 4);
}
// getCtorSection - Get the static constructor section // getCtorSection - Get the static constructor section
ELFSection &ELFWriter::getCtorSection() { ELFSection &ELFWriter::getCtorSection() {
const MCSectionELF *Ctor = (const MCSectionELF *)TLOF.getStaticCtorSection(); const MCSectionELF *Ctor = (const MCSectionELF *)TLOF.getStaticCtorSection();
return getSection(Ctor->getSectionName(), ELFSection::SHT_PROGBITS, return getSection(Ctor->getSectionName(), Ctor->getType(), Ctor->getFlags());
getElfSectionFlags(Ctor->getKind()));
} }
// getDtorSection - Get the static destructor section // getDtorSection - Get the static destructor section
ELFSection &ELFWriter::getDtorSection() { ELFSection &ELFWriter::getDtorSection() {
const MCSectionELF *Dtor = (const MCSectionELF *)TLOF.getStaticDtorSection(); const MCSectionELF *Dtor = (const MCSectionELF *)TLOF.getStaticDtorSection();
return getSection(Dtor->getSectionName(), ELFSection::SHT_PROGBITS, return getSection(Dtor->getSectionName(), Dtor->getType(), Dtor->getFlags());
getElfSectionFlags(Dtor->getKind()));
} }
// getTextSection - Get the text section for the specified function // getTextSection - Get the text section for the specified function
ELFSection &ELFWriter::getTextSection(Function *F) { ELFSection &ELFWriter::getTextSection(Function *F) {
const MCSectionELF *Text = const MCSectionELF *Text =
(const MCSectionELF *)TLOF.SectionForGlobal(F, Mang, TM); (const MCSectionELF *)TLOF.SectionForGlobal(F, Mang, TM);
return getSection(Text->getSectionName(), ELFSection::SHT_PROGBITS, return getSection(Text->getSectionName(), Text->getType(), Text->getFlags());
getElfSectionFlags(Text->getKind()));
} }
// getJumpTableSection - Get a read only section for constants when // getJumpTableSection - Get a read only section for constants when
@@ -203,9 +212,7 @@ ELFSection &ELFWriter::getTextSection(Function *F) {
ELFSection &ELFWriter::getJumpTableSection() { ELFSection &ELFWriter::getJumpTableSection() {
const MCSectionELF *JT = const MCSectionELF *JT =
(const MCSectionELF *)TLOF.getSectionForConstant(SectionKind::getReadOnly()); (const MCSectionELF *)TLOF.getSectionForConstant(SectionKind::getReadOnly());
return getSection(JT->getSectionName(), return getSection(JT->getSectionName(), JT->getType(), JT->getFlags(),
ELFSection::SHT_PROGBITS,
getElfSectionFlags(JT->getKind()),
TM.getTargetData()->getPointerABIAlignment()); TM.getTargetData()->getPointerABIAlignment());
} }
@@ -230,23 +237,22 @@ ELFSection &ELFWriter::getConstantPoolSection(MachineConstantPoolEntry &CPE) {
const MCSectionELF *CPSect = const MCSectionELF *CPSect =
(const MCSectionELF *)TLOF.getSectionForConstant(Kind); (const MCSectionELF *)TLOF.getSectionForConstant(Kind);
return getSection(CPSect->getSectionName(), return getSection(CPSect->getSectionName(), CPSect->getType(),
ELFSection::SHT_PROGBITS, CPSect->getFlags(), CPE.getAlignment());
getElfSectionFlags(Kind),
CPE.getAlignment());
} }
// getRelocSection - Return the relocation section of section 'S'. 'RelA' // getRelocSection - Return the relocation section of section 'S'. 'RelA'
// is true if the relocation section contains entries with addends. // is true if the relocation section contains entries with addends.
ELFSection &ELFWriter::getRelocSection(ELFSection &S) { ELFSection &ELFWriter::getRelocSection(ELFSection &S) {
unsigned SectionHeaderTy = TEW->hasRelocationAddend() ? unsigned SectionType = TEW->hasRelocationAddend() ?
ELFSection::SHT_RELA : ELFSection::SHT_REL; ELFSection::SHT_RELA : ELFSection::SHT_REL;
std::string RelSName(".rel");
if (TEW->hasRelocationAddend())
RelSName.append("a");
RelSName.append(S.getName());
return getSection(RelSName, SectionHeaderTy, 0, TEW->getPrefELFAlignment()); std::string SectionName(".rel");
if (TEW->hasRelocationAddend())
SectionName.append("a");
SectionName.append(S.getName());
return getSection(SectionName, SectionType, 0, TEW->getPrefELFAlignment());
} }
// getGlobalELFVisibility - Returns the ELF specific visibility type // getGlobalELFVisibility - Returns the ELF specific visibility type
@@ -286,54 +292,32 @@ unsigned ELFWriter::getGlobalELFType(const GlobalValue *GV) {
return ELFSym::STT_OBJECT; return ELFSym::STT_OBJECT;
} }
// getElfSectionFlags - Get the ELF Section Header flags based // IsELFUndefSym - True if the global value must be marked as a symbol
// on the flags defined in SectionKind.h. // which points to a SHN_UNDEF section. This means that the symbol has
unsigned ELFWriter::getElfSectionFlags(SectionKind Kind, bool IsAlloc) { // no definition on the module.
unsigned ElfSectionFlags = 0; static bool IsELFUndefSym(const GlobalValue *GV) {
if (IsAlloc)
ElfSectionFlags |= ELFSection::SHF_ALLOC;
if (Kind.isText())
ElfSectionFlags |= ELFSection::SHF_EXECINSTR;
if (Kind.isWriteable())
ElfSectionFlags |= ELFSection::SHF_WRITE;
if (Kind.isMergeableConst() || Kind.isMergeableCString())
ElfSectionFlags |= ELFSection::SHF_MERGE;
if (Kind.isThreadLocal())
ElfSectionFlags |= ELFSection::SHF_TLS;
if (Kind.isMergeableCString())
ElfSectionFlags |= ELFSection::SHF_STRINGS;
return ElfSectionFlags;
}
// isUndefOrNull - The constant is either a null initialized value or an
// undefined one.
static bool isUndefOrNull(const Constant *CV) {
return (CV->isNullValue() || isa<UndefValue>(CV));
}
// isELFUndefSym - the symbol has no section and must be placed in
// the symbol table with a reference to the null section.
static bool isELFUndefSym(const GlobalValue *GV) {
// Functions which make up until this point references are an undef symbol
return GV->isDeclaration() || (isa<Function>(GV)); return GV->isDeclaration() || (isa<Function>(GV));
} }
// isELFBssSym - for an undef or null value, the symbol must go to a bss // AddToSymbolList - Update the symbol lookup and If the symbol is
// section if it's not weak for linker, otherwise it's a common sym. // private add it to PrivateSyms list, otherwise to SymbolList.
static bool isELFBssSym(const GlobalVariable *GV, SectionKind Kind) { void ELFWriter::AddToSymbolList(ELFSym *GblSym) {
const Constant *CV = GV->getInitializer(); assert(GblSym->isGlobalValue() && "Symbol must be a global value");
return (!Kind.isMergeableCString() && const GlobalValue *GV = GblSym->getGlobalValue();
isUndefOrNull(CV) && if (GV->hasPrivateLinkage()) {
!GV->isWeakForLinker()); // For a private symbols, keep track of the index inside
} // the private list since it will never go to the symbol
// table and won't be patched up later.
// isELFCommonSym - for an undef or null value, the symbol must go to a PrivateSyms.push_back(GblSym);
// common section if it's weak for linker, otherwise bss. GblSymLookup[GV] = PrivateSyms.size()-1;
static bool isELFCommonSym(const GlobalVariable *GV) { } else {
return (isUndefOrNull(GV->getInitializer()) && GV->isWeakForLinker()); // Non private symbol are left with zero indices until
// they are patched up during the symbol table emition
// (where the indicies are created).
SymbolList.push_back(GblSym);
GblSymLookup[GV] = 0;
}
} }
// EmitGlobal - Choose the right section for global and emit it // EmitGlobal - Choose the right section for global and emit it
@@ -346,13 +330,12 @@ void ELFWriter::EmitGlobal(const GlobalValue *GV) {
// Handle ELF Bind, Visibility and Type for the current symbol // Handle ELF Bind, Visibility and Type for the current symbol
unsigned SymBind = getGlobalELFBinding(GV); unsigned SymBind = getGlobalELFBinding(GV);
unsigned SymType = getGlobalELFType(GV); unsigned SymType = getGlobalELFType(GV);
bool IsUndefSym = IsELFUndefSym(GV);
// All undef symbols have the same binding, type and visibily and ELFSym *GblSym = IsUndefSym ? ELFSym::getUndefGV(GV, SymBind)
// are classified regardless of their type.
ELFSym *GblSym = isELFUndefSym(GV) ? ELFSym::getUndefGV(GV, SymBind)
: ELFSym::getGV(GV, SymBind, SymType, getGlobalELFVisibility(GV)); : ELFSym::getGV(GV, SymBind, SymType, getGlobalELFVisibility(GV));
if (!isELFUndefSym(GV)) { if (!IsUndefSym) {
assert(isa<GlobalVariable>(GV) && "GV not a global variable!"); assert(isa<GlobalVariable>(GV) && "GV not a global variable!");
const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
@@ -363,8 +346,9 @@ void ELFWriter::EmitGlobal(const GlobalValue *GV) {
// Get the ELF section where this global belongs from TLOF // Get the ELF section where this global belongs from TLOF
const MCSectionELF *S = const MCSectionELF *S =
(const MCSectionELF *)TLOF.SectionForGlobal(GV, Mang, TM); (const MCSectionELF *)TLOF.SectionForGlobal(GV, Mang, TM);
ELFSection &ES =
getSection(S->getSectionName(), S->getType(), S->getFlags());
SectionKind Kind = S->getKind(); SectionKind Kind = S->getKind();
unsigned SectionFlags = getElfSectionFlags(Kind);
// The symbol align should update the section alignment if needed // The symbol align should update the section alignment if needed
const TargetData *TD = TM.getTargetData(); const TargetData *TD = TM.getTargetData();
@@ -372,20 +356,16 @@ void ELFWriter::EmitGlobal(const GlobalValue *GV) {
unsigned Size = TD->getTypeAllocSize(GVar->getInitializer()->getType()); unsigned Size = TD->getTypeAllocSize(GVar->getInitializer()->getType());
GblSym->Size = Size; GblSym->Size = Size;
if (isELFCommonSym(GVar)) { if (S->IsCommon()) { // Symbol must go to a common section
GblSym->SectionIdx = ELFSection::SHN_COMMON; GblSym->SectionIdx = ELFSection::SHN_COMMON;
getSection(S->getSectionName(),
ELFSection::SHT_NOBITS, SectionFlags, 1);
// A new linkonce section is created for each global in the // A new linkonce section is created for each global in the
// common section, the default alignment is 1 and the symbol // common section, the default alignment is 1 and the symbol
// value contains its alignment. // value contains its alignment.
ES.Align = 1;
GblSym->Value = Align; GblSym->Value = Align;
} else if (isELFBssSym(GVar, Kind)) { } else if (Kind.isBSS() || Kind.isThreadBSS()) { // Symbol goes to BSS.
ELFSection &ES =
getSection(S->getSectionName(), ELFSection::SHT_NOBITS,
SectionFlags);
GblSym->SectionIdx = ES.SectionIdx; GblSym->SectionIdx = ES.SectionIdx;
// Update the size with alignment and the next object can // Update the size with alignment and the next object can
@@ -399,9 +379,6 @@ void ELFWriter::EmitGlobal(const GlobalValue *GV) {
ES.Size += Size; ES.Size += Size;
} else { // The symbol must go to some kind of data section } else { // The symbol must go to some kind of data section
ELFSection &ES =
getSection(S->getSectionName(), ELFSection::SHT_PROGBITS,
SectionFlags);
GblSym->SectionIdx = ES.SectionIdx; GblSym->SectionIdx = ES.SectionIdx;
// GblSym->Value should contain the symbol offset inside the section, // GblSym->Value should contain the symbol offset inside the section,
@@ -415,18 +392,7 @@ void ELFWriter::EmitGlobal(const GlobalValue *GV) {
} }
} }
if (GV->hasPrivateLinkage()) { AddToSymbolList(GblSym);
// For a private symbols, keep track of the index inside the
// private list since it will never go to the symbol table and
// won't be patched up later.
PrivateSyms.push_back(GblSym);
GblSymLookup[GV] = PrivateSyms.size()-1;
} else {
// Non private symbol are left with zero indices until they are patched
// up during the symbol table emition (where the indicies are created).
SymbolList.push_back(GblSym);
GblSymLookup[GV] = 0;
}
} }
void ELFWriter::EmitGlobalConstantStruct(const ConstantStruct *CVS, void ELFWriter::EmitGlobalConstantStruct(const ConstantStruct *CVS,

View File

@@ -146,35 +146,14 @@ namespace llvm {
/// present in the SymbolList. /// present in the SymbolList.
std::vector<ELFSym*> PrivateSyms; std::vector<ELFSym*> PrivateSyms;
// Remove tab from section name prefix. This is necessary becase TAI
// sometimes return a section name prefixed with elf unused chars. This is
// a little bit dirty. FIXME: find a better approach, maybe add more
// methods to TAI to get the clean name?
void fixNameForSection(std::string &Name) {
size_t Pos = Name.find("\t");
if (Pos != std::string::npos)
Name.erase(Pos, 1);
Pos = Name.find(".section ");
if (Pos != std::string::npos)
Name.erase(Pos, 9);
Pos = Name.find("\n");
if (Pos != std::string::npos)
Name.erase(Pos, 1);
}
/// getSection - Return the section with the specified name, creating a new /// getSection - Return the section with the specified name, creating a new
/// section if one does not already exist. /// section if one does not already exist.
ELFSection &getSection(const std::string &Name, unsigned Type, ELFSection &getSection(const std::string &Name, unsigned Type,
unsigned Flags = 0, unsigned Align = 0) { unsigned Flags = 0, unsigned Align = 0) {
std::string SName(Name); ELFSection *&SN = SectionLookup[Name];
fixNameForSection(SName);
ELFSection *&SN = SectionLookup[SName];
if (SN) return *SN; if (SN) return *SN;
SectionList.push_back(new ELFSection(SName, isLittleEndian, is64Bit)); SectionList.push_back(new ELFSection(Name, isLittleEndian, is64Bit));
SN = SectionList.back(); SN = SectionList.back();
SN->SectionIdx = NumSections++; SN->SectionIdx = NumSections++;
SN->Type = Type; SN->Type = Type;
@@ -200,20 +179,12 @@ namespace llvm {
return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1); return getSection(".shstrtab", ELFSection::SHT_STRTAB, 0, 1);
} }
ELFSection &getDataSection() {
return getSection(".data", ELFSection::SHT_PROGBITS,
ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
}
ELFSection &getBSSSection() {
return getSection(".bss", ELFSection::SHT_NOBITS,
ELFSection::SHF_WRITE | ELFSection::SHF_ALLOC, 4);
}
ELFSection &getNullSection() { ELFSection &getNullSection() {
return getSection("", ELFSection::SHT_NULL, 0); return getSection("", ELFSection::SHT_NULL, 0);
} }
ELFSection &getDataSection();
ELFSection &getBSSSection();
ELFSection &getCtorSection(); ELFSection &getCtorSection();
ELFSection &getDtorSection(); ELFSection &getDtorSection();
ELFSection &getJumpTableSection(); ELFSection &getJumpTableSection();
@@ -225,17 +196,21 @@ namespace llvm {
unsigned getGlobalELFBinding(const GlobalValue *GV); unsigned getGlobalELFBinding(const GlobalValue *GV);
unsigned getGlobalELFType(const GlobalValue *GV); unsigned getGlobalELFType(const GlobalValue *GV);
unsigned getGlobalELFVisibility(const GlobalValue *GV); unsigned getGlobalELFVisibility(const GlobalValue *GV);
unsigned getElfSectionFlags(SectionKind Kind, bool IsAlloc = true);
// addGlobalSymbol - Add a global to be processed and to // AddPendingGlobalSymbol - Add a global to be processed and to
// the global symbol lookup, use a zero index because the table // the global symbol lookup, use a zero index because the table
// index will be determined later. // index will be determined later.
void addGlobalSymbol(const GlobalValue *GV, bool AddToLookup = false); void AddPendingGlobalSymbol(const GlobalValue *GV,
bool AddToLookup = false);
// addExternalSymbol - Add the external to be processed and to the // AddPendingExternalSymbol - Add the external to be processed
// external symbol lookup, use a zero index because the symbol // and to the external symbol lookup, use a zero index because
// table index will be determined later // the symbol table index will be determined later.
void addExternalSymbol(const char *External); void AddPendingExternalSymbol(const char *External);
// AddToSymbolList - Update the symbol lookup and If the symbol is
// private add it to PrivateSyms list, otherwise to SymbolList.
void AddToSymbolList(ELFSym *GblSym);
// As we complete the ELF file, we need to update fields in the ELF header // As we complete the ELF file, we need to update fields in the ELF header
// (e.g. the location of the section table). These members keep track of // (e.g. the location of the section table). These members keep track of