Index: ELF/Symbols.h =================================================================== --- ELF/Symbols.h +++ ELF/Symbols.h @@ -390,17 +390,16 @@ // Some linker-generated symbols need to be created as // DefinedRegular symbols. template struct ElfSym { + typedef std::pair *, DefinedRegular *> LinkerSym; + // The content for _etext and etext symbols. - static DefinedRegular *Etext; - static DefinedRegular *Etext2; + static LinkerSym Etext; // The content for _edata and edata symbols. - static DefinedRegular *Edata; - static DefinedRegular *Edata2; + static LinkerSym Edata; // The content for _end and end symbols. - static DefinedRegular *End; - static DefinedRegular *End2; + static LinkerSym End; // The content for _gp symbol for MIPS target. static SymbolBody *MipsGp; @@ -414,12 +413,10 @@ static SymbolBody *RelaIpltEnd; }; -template DefinedRegular *ElfSym::Etext; -template DefinedRegular *ElfSym::Etext2; -template DefinedRegular *ElfSym::Edata; -template DefinedRegular *ElfSym::Edata2; -template DefinedRegular *ElfSym::End; -template DefinedRegular *ElfSym::End2; +template typename ElfSym::LinkerSym ElfSym::Etext; +template typename ElfSym::LinkerSym ElfSym::Edata; +template typename ElfSym::LinkerSym ElfSym::End; + template SymbolBody *ElfSym::MipsGp; template SymbolBody *ElfSym::MipsLocalGp; template SymbolBody *ElfSym::MipsGpDisp; Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1113,9 +1113,8 @@ if (!isOutputDynamic()) Symtab.addIgnored("__tls_get_addr"); - auto Define = [this](StringRef S, DefinedRegular *&Sym, - DefinedRegular *&Sym2) { - Sym = Symtab.addIgnored(S, STV_DEFAULT); + auto Define = [this](StringRef S, ElfSym::LinkerSym &Sym) { + Sym.first = Symtab.addIgnored(S, STV_DEFAULT); // The name without the underscore is not a reserved name, // so it is defined only when there is a reference against it. @@ -1123,12 +1122,12 @@ S = S.substr(1); if (SymbolBody *B = Symtab.find(S)) if (B->isUndefined()) - Sym2 = Symtab.addAbsolute(S, STV_DEFAULT); + Sym.second = Symtab.addAbsolute(S, STV_DEFAULT); }; - Define("_end", ElfSym::End, ElfSym::End2); - Define("_etext", ElfSym::Etext, ElfSym::Etext2); - Define("_edata", ElfSym::Edata, ElfSym::Edata2); + Define("_end", ElfSym::End); + Define("_etext", ElfSym::Etext); + Define("_edata", ElfSym::Edata); } // Sort input sections by section name suffixes for @@ -1663,28 +1662,23 @@ // _etext is the first location after the last read-only loadable segment. // _edata is the first location after the last read-write loadable segment. // _end is the first location after the uninitialized data region. + auto Assign = [](ElfSym::LinkerSym &Sym, uintX_t Val) { + if (Sym.first) + Sym.first->Value = Val; + if (Sym.second) + Sym.second->Value = Val; + }; for (Phdr &P : Phdrs) { Elf_Phdr &H = P.H; if (H.p_type != PT_LOAD) continue; - uintX_t Val = H.p_vaddr + H.p_memsz; - if (ElfSym::End) - ElfSym::End->Value = Val; - if (ElfSym::End2) - ElfSym::End2->Value = Val; - - Val = H.p_vaddr + H.p_filesz; - if (H.p_flags & PF_W) { - if (ElfSym::Edata) - ElfSym::Edata->Value = Val; - if (ElfSym::Edata2) - ElfSym::Edata2->Value = Val; - } else { - if (ElfSym::Etext) - ElfSym::Etext->Value = Val; - if (ElfSym::Etext2) - ElfSym::Etext2->Value = Val; - } + Assign(ElfSym::End, H.p_vaddr + H.p_memsz); + + uintX_t Val = H.p_vaddr + H.p_filesz; + if (H.p_flags & PF_W) + Assign(ElfSym::Edata, Val); + else + Assign(ElfSym::Etext, Val); } }