From d163e8b14c8aa5bbbb129e3f0dffdbe7213a3c72 Mon Sep 17 00:00:00 2001 From: Bruno Cardoso Lopes Date: Thu, 13 Aug 2009 21:25:27 +0000 Subject: [PATCH] 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 --- lib/CodeGen/ELF.h | 8 +- lib/CodeGen/ELFCodeEmitter.cpp | 6 +- lib/CodeGen/ELFWriter.cpp | 162 +++++++++++++-------------------- lib/CodeGen/ELFWriter.h | 57 ++++-------- 4 files changed, 87 insertions(+), 146 deletions(-) diff --git a/lib/CodeGen/ELF.h b/lib/CodeGen/ELF.h index c9ec9b1912b..b466e89cb26 100644 --- a/lib/CodeGen/ELF.h +++ b/lib/CodeGen/ELF.h @@ -74,19 +74,19 @@ namespace llvm { }; unsigned SourceType; - bool isGlobalValue() { return SourceType == isGV; } - bool isExternalSym() { return SourceType == isExtSym; } + bool isGlobalValue() const { return SourceType == isGV; } + bool isExternalSym() const { return SourceType == isExtSym; } // getGlobalValue - If this is a global value which originated the // elf symbol, return a reference to it. - const GlobalValue *getGlobalValue() { + const GlobalValue *getGlobalValue() const { assert(SourceType == isGV && "This is not a global value"); return Source.GV; }; // getExternalSym - If this is an external symbol which originated the // elf symbol, return a reference to it. - const char *getExternalSymbol() { + const char *getExternalSymbol() const { assert(SourceType == isExtSym && "This is not an external symbol"); return Source.Ext; }; diff --git a/lib/CodeGen/ELFCodeEmitter.cpp b/lib/CodeGen/ELFCodeEmitter.cpp index 61a9d5a57b3..38cf826fbe3 100644 --- a/lib/CodeGen/ELFCodeEmitter.cpp +++ b/lib/CodeGen/ELFCodeEmitter.cpp @@ -73,7 +73,7 @@ bool ELFCodeEmitter::finishFunction(MachineFunction &MF) { EW.getGlobalELFVisibility(F)); FnSym->SectionIdx = ES->SectionIdx; FnSym->Size = ES->getCurrentPCOffset()-FnStartOff; - EW.addGlobalSymbol(F, true); + EW.AddPendingGlobalSymbol(F, true); // Offset from start of Section FnSym->Value = FnStartOff; @@ -102,9 +102,9 @@ bool ELFCodeEmitter::finishFunction(MachineFunction &MF) { MachineRelocation &MR = Relocations[i]; intptr_t Addr; if (MR.isGlobalValue()) { - EW.addGlobalSymbol(MR.getGlobalValue()); + EW.AddPendingGlobalSymbol(MR.getGlobalValue()); } else if (MR.isExternalSymbol()) { - EW.addExternalSymbol(MR.getExternalSymbol()); + EW.AddPendingExternalSymbol(MR.getExternalSymbol()); } else if (MR.isBasicBlock()) { Addr = getMachineBasicBlockAddress(MR.getBasicBlock()); MR.setConstantVal(ES->SectionIdx); diff --git a/lib/CodeGen/ELFWriter.cpp b/lib/CodeGen/ELFWriter.cpp index 4ad2f8bd53e..1518a34acb6 100644 --- a/lib/CodeGen/ELFWriter.cpp +++ b/lib/CodeGen/ELFWriter.cpp @@ -159,43 +159,52 @@ bool ELFWriter::doInitialization(Module &M) { return false; } -// addGlobalSymbol - Add a global to be processed and to the global symbol -// lookup, use a zero index because the table index will be determined later. -void ELFWriter::addGlobalSymbol(const GlobalValue *GV, - bool AddToLookup /* = false */) { +// AddPendingGlobalSymbol - Add a global to be processed and to +// the global symbol lookup, use a zero index because the table +// index will be determined later. +void ELFWriter::AddPendingGlobalSymbol(const GlobalValue *GV, + bool AddToLookup /* = false */) { PendingGlobals.insert(GV); if (AddToLookup) GblSymLookup[GV] = 0; } -// addExternalSymbol - Add the external to be processed and to the -// external symbol lookup, use a zero index because the symbol -// table index will be determined later -void ELFWriter::addExternalSymbol(const char *External) { +// AddPendingExternalSymbol - Add the external to be processed +// and to the external symbol lookup, use a zero index because +// the symbol table index will be determined later. +void ELFWriter::AddPendingExternalSymbol(const char *External) { PendingExternals.insert(External); 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 ELFSection &ELFWriter::getCtorSection() { const MCSectionELF *Ctor = (const MCSectionELF *)TLOF.getStaticCtorSection(); - return getSection(Ctor->getSectionName(), ELFSection::SHT_PROGBITS, - getElfSectionFlags(Ctor->getKind())); + return getSection(Ctor->getSectionName(), Ctor->getType(), Ctor->getFlags()); } // getDtorSection - Get the static destructor section ELFSection &ELFWriter::getDtorSection() { const MCSectionELF *Dtor = (const MCSectionELF *)TLOF.getStaticDtorSection(); - return getSection(Dtor->getSectionName(), ELFSection::SHT_PROGBITS, - getElfSectionFlags(Dtor->getKind())); + return getSection(Dtor->getSectionName(), Dtor->getType(), Dtor->getFlags()); } // getTextSection - Get the text section for the specified function ELFSection &ELFWriter::getTextSection(Function *F) { const MCSectionELF *Text = (const MCSectionELF *)TLOF.SectionForGlobal(F, Mang, TM); - return getSection(Text->getSectionName(), ELFSection::SHT_PROGBITS, - getElfSectionFlags(Text->getKind())); + return getSection(Text->getSectionName(), Text->getType(), Text->getFlags()); } // getJumpTableSection - Get a read only section for constants when @@ -203,9 +212,7 @@ ELFSection &ELFWriter::getTextSection(Function *F) { ELFSection &ELFWriter::getJumpTableSection() { const MCSectionELF *JT = (const MCSectionELF *)TLOF.getSectionForConstant(SectionKind::getReadOnly()); - return getSection(JT->getSectionName(), - ELFSection::SHT_PROGBITS, - getElfSectionFlags(JT->getKind()), + return getSection(JT->getSectionName(), JT->getType(), JT->getFlags(), TM.getTargetData()->getPointerABIAlignment()); } @@ -230,23 +237,22 @@ ELFSection &ELFWriter::getConstantPoolSection(MachineConstantPoolEntry &CPE) { const MCSectionELF *CPSect = (const MCSectionELF *)TLOF.getSectionForConstant(Kind); - return getSection(CPSect->getSectionName(), - ELFSection::SHT_PROGBITS, - getElfSectionFlags(Kind), - CPE.getAlignment()); + return getSection(CPSect->getSectionName(), CPSect->getType(), + CPSect->getFlags(), CPE.getAlignment()); } // getRelocSection - Return the relocation section of section 'S'. 'RelA' // is true if the relocation section contains entries with addends. ELFSection &ELFWriter::getRelocSection(ELFSection &S) { - unsigned SectionHeaderTy = TEW->hasRelocationAddend() ? - ELFSection::SHT_RELA : ELFSection::SHT_REL; - std::string RelSName(".rel"); + unsigned SectionType = TEW->hasRelocationAddend() ? + ELFSection::SHT_RELA : ELFSection::SHT_REL; + + std::string SectionName(".rel"); if (TEW->hasRelocationAddend()) - RelSName.append("a"); - RelSName.append(S.getName()); + SectionName.append("a"); + SectionName.append(S.getName()); - return getSection(RelSName, SectionHeaderTy, 0, TEW->getPrefELFAlignment()); + return getSection(SectionName, SectionType, 0, TEW->getPrefELFAlignment()); } // getGlobalELFVisibility - Returns the ELF specific visibility type @@ -286,54 +292,32 @@ unsigned ELFWriter::getGlobalELFType(const GlobalValue *GV) { return ELFSym::STT_OBJECT; } -// getElfSectionFlags - Get the ELF Section Header flags based -// on the flags defined in SectionKind.h. -unsigned ELFWriter::getElfSectionFlags(SectionKind Kind, bool IsAlloc) { - unsigned ElfSectionFlags = 0; - - 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(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 +// IsELFUndefSym - True if the global value must be marked as a symbol +// which points to a SHN_UNDEF section. This means that the symbol has +// no definition on the module. +static bool IsELFUndefSym(const GlobalValue *GV) { return GV->isDeclaration() || (isa(GV)); } -// isELFBssSym - for an undef or null value, the symbol must go to a bss -// section if it's not weak for linker, otherwise it's a common sym. -static bool isELFBssSym(const GlobalVariable *GV, SectionKind Kind) { - const Constant *CV = GV->getInitializer(); +// AddToSymbolList - Update the symbol lookup and If the symbol is +// private add it to PrivateSyms list, otherwise to SymbolList. +void ELFWriter::AddToSymbolList(ELFSym *GblSym) { + assert(GblSym->isGlobalValue() && "Symbol must be a global value"); - return (!Kind.isMergeableCString() && - isUndefOrNull(CV) && - !GV->isWeakForLinker()); -} - -// isELFCommonSym - for an undef or null value, the symbol must go to a -// common section if it's weak for linker, otherwise bss. -static bool isELFCommonSym(const GlobalVariable *GV) { - return (isUndefOrNull(GV->getInitializer()) && GV->isWeakForLinker()); + const GlobalValue *GV = GblSym->getGlobalValue(); + if (GV->hasPrivateLinkage()) { + // 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; + } } // 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 unsigned SymBind = getGlobalELFBinding(GV); unsigned SymType = getGlobalELFType(GV); + bool IsUndefSym = IsELFUndefSym(GV); - // All undef symbols have the same binding, type and visibily and - // are classified regardless of their type. - ELFSym *GblSym = isELFUndefSym(GV) ? ELFSym::getUndefGV(GV, SymBind) + ELFSym *GblSym = IsUndefSym ? ELFSym::getUndefGV(GV, SymBind) : ELFSym::getGV(GV, SymBind, SymType, getGlobalELFVisibility(GV)); - if (!isELFUndefSym(GV)) { + if (!IsUndefSym) { assert(isa(GV) && "GV not a global variable!"); const GlobalVariable *GVar = dyn_cast(GV); @@ -363,8 +346,9 @@ void ELFWriter::EmitGlobal(const GlobalValue *GV) { // Get the ELF section where this global belongs from TLOF const MCSectionELF *S = (const MCSectionELF *)TLOF.SectionForGlobal(GV, Mang, TM); + ELFSection &ES = + getSection(S->getSectionName(), S->getType(), S->getFlags()); SectionKind Kind = S->getKind(); - unsigned SectionFlags = getElfSectionFlags(Kind); // The symbol align should update the section alignment if needed const TargetData *TD = TM.getTargetData(); @@ -372,20 +356,16 @@ void ELFWriter::EmitGlobal(const GlobalValue *GV) { unsigned Size = TD->getTypeAllocSize(GVar->getInitializer()->getType()); GblSym->Size = Size; - if (isELFCommonSym(GVar)) { + if (S->IsCommon()) { // Symbol must go to a common section GblSym->SectionIdx = ELFSection::SHN_COMMON; - getSection(S->getSectionName(), - ELFSection::SHT_NOBITS, SectionFlags, 1); // A new linkonce section is created for each global in the // common section, the default alignment is 1 and the symbol // value contains its alignment. + ES.Align = 1; GblSym->Value = Align; - } else if (isELFBssSym(GVar, Kind)) { - ELFSection &ES = - getSection(S->getSectionName(), ELFSection::SHT_NOBITS, - SectionFlags); + } else if (Kind.isBSS() || Kind.isThreadBSS()) { // Symbol goes to BSS. GblSym->SectionIdx = ES.SectionIdx; // Update the size with alignment and the next object can @@ -399,9 +379,6 @@ void ELFWriter::EmitGlobal(const GlobalValue *GV) { ES.Size += Size; } 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->Value should contain the symbol offset inside the section, @@ -415,18 +392,7 @@ void ELFWriter::EmitGlobal(const GlobalValue *GV) { } } - if (GV->hasPrivateLinkage()) { - // 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; - } + AddToSymbolList(GblSym); } void ELFWriter::EmitGlobalConstantStruct(const ConstantStruct *CVS, diff --git a/lib/CodeGen/ELFWriter.h b/lib/CodeGen/ELFWriter.h index ae14313db6a..2f8f625d967 100644 --- a/lib/CodeGen/ELFWriter.h +++ b/lib/CodeGen/ELFWriter.h @@ -146,35 +146,14 @@ namespace llvm { /// present in the SymbolList. std::vector 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 /// section if one does not already exist. ELFSection &getSection(const std::string &Name, unsigned Type, unsigned Flags = 0, unsigned Align = 0) { - std::string SName(Name); - fixNameForSection(SName); - - ELFSection *&SN = SectionLookup[SName]; + ELFSection *&SN = SectionLookup[Name]; if (SN) return *SN; - SectionList.push_back(new ELFSection(SName, isLittleEndian, is64Bit)); + SectionList.push_back(new ELFSection(Name, isLittleEndian, is64Bit)); SN = SectionList.back(); SN->SectionIdx = NumSections++; SN->Type = Type; @@ -200,20 +179,12 @@ namespace llvm { 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() { return getSection("", ELFSection::SHT_NULL, 0); } + ELFSection &getDataSection(); + ELFSection &getBSSSection(); ELFSection &getCtorSection(); ELFSection &getDtorSection(); ELFSection &getJumpTableSection(); @@ -225,17 +196,21 @@ namespace llvm { unsigned getGlobalELFBinding(const GlobalValue *GV); unsigned getGlobalELFType(const GlobalValue *GV); unsigned getGlobalELFVisibility(const GlobalValue *GV); - unsigned getElfSectionFlags(SectionKind Kind, bool IsAlloc = true); - // addGlobalSymbol - Add a global to be processed and to - // the global symbol lookup, use a zero index because the table + // AddPendingGlobalSymbol - Add a global to be processed and to + // the global symbol lookup, use a zero index because the table // 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 - // external symbol lookup, use a zero index because the symbol - // table index will be determined later - void addExternalSymbol(const char *External); + // AddPendingExternalSymbol - Add the external to be processed + // and to the external symbol lookup, use a zero index because + // the symbol table index will be determined later. + 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 // (e.g. the location of the section table). These members keep track of -- 2.34.1