@@ -1630,8 +1630,9 @@ uint32_t DynamicReloc::getSymIndex(SymbolTableBaseSection *symTab) const {
16301630
16311631 size_t index = symTab->getSymbolIndex (*sym);
16321632 assert ((index != 0 ||
1633- (type != ctx.target ->gotRel && type != ctx.target ->pltRel ) ||
1634- !ctx.mainPart ->dynSymTab ->getParent ()) &&
1633+ (type != symTab->ctx .target ->gotRel &&
1634+ type != symTab->ctx .target ->pltRel ) ||
1635+ !symTab->ctx .mainPart ->dynSymTab ->getParent ()) &&
16351636 " GOT or PLT relocation must refer to symbol in dynamic symbol table" );
16361637 return index;
16371638}
@@ -2241,8 +2242,8 @@ SymbolTableSection<ELFT>::SymbolTableSection(Ctx &ctx,
22412242 this ->entsize = sizeof (Elf_Sym);
22422243}
22432244
2244- static BssSection *getCommonSec (Symbol *sym) {
2245- if (ctx. arg . relocatable )
2245+ static BssSection *getCommonSec (bool relocatable, Symbol *sym) {
2246+ if (relocatable)
22462247 if (auto *d = dyn_cast<Defined>(sym))
22472248 return dyn_cast_or_null<BssSection>(d->section );
22482249 return nullptr ;
@@ -2264,7 +2265,7 @@ template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *buf) {
22642265 buf += sizeof (Elf_Sym);
22652266
22662267 auto *eSym = reinterpret_cast <Elf_Sym *>(buf);
2267-
2268+ bool relocatable = ctx. arg . relocatable ;
22682269 for (SymbolTableEntry &ent : symbols) {
22692270 Symbol *sym = ent.sym ;
22702271 bool isDefinedHere = type == SHT_SYMTAB || sym->partition == partition;
@@ -2274,7 +2275,7 @@ template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *buf) {
22742275 eSym->setBindingAndType (sym->binding , sym->type );
22752276 eSym->st_other = sym->stOther ;
22762277
2277- if (BssSection *commonSec = getCommonSec (sym)) {
2278+ if (BssSection *commonSec = getCommonSec (relocatable, sym)) {
22782279 // When -r is specified, a COMMON symbol is not allocated. Its st_shndx
22792280 // holds SHN_COMMON and st_value holds the alignment.
22802281 eSym->st_shndx = SHN_COMMON;
@@ -2347,8 +2348,10 @@ void SymtabShndxSection::writeTo(uint8_t *buf) {
23472348 // SHN_XINDEX, we need to write actual index, otherwise, we must write
23482349 // SHN_UNDEF(0).
23492350 buf += 4 ; // Ignore .symtab[0] entry.
2351+ bool relocatable = ctx.arg .relocatable ;
23502352 for (const SymbolTableEntry &entry : ctx.in .symTab ->getSymbols ()) {
2351- if (!getCommonSec (entry.sym ) && getSymSectionIndex (entry.sym ) == SHN_XINDEX)
2353+ if (!getCommonSec (relocatable, entry.sym ) &&
2354+ getSymSectionIndex (entry.sym ) == SHN_XINDEX)
23522355 write32 (buf, entry.sym ->getOutputSection ()->sectionIndex );
23532356 buf += 4 ;
23542357 }
@@ -4384,7 +4387,8 @@ static uint8_t getAbiVersion(Ctx &ctx) {
43844387 return 0 ;
43854388}
43864389
4387- template <typename ELFT> void elf::writeEhdr (uint8_t *buf, Partition &part) {
4390+ template <typename ELFT>
4391+ void elf::writeEhdr (Ctx &ctx, uint8_t *buf, Partition &part) {
43884392 memcpy (buf, " \177 ELF" , 4 );
43894393
43904394 auto *eHdr = reinterpret_cast <typename ELFT::Ehdr *>(buf);
@@ -4434,7 +4438,7 @@ size_t PartitionElfHeaderSection<ELFT>::getSize() const {
44344438
44354439template <typename ELFT>
44364440void PartitionElfHeaderSection<ELFT>::writeTo(uint8_t *buf) {
4437- writeEhdr<ELFT>(buf, getPartition ());
4441+ writeEhdr<ELFT>(ctx, buf, getPartition ());
44384442
44394443 // Loadable partitions are always ET_DYN.
44404444 auto *eHdr = reinterpret_cast <typename ELFT::Ehdr *>(buf);
@@ -4970,10 +4974,10 @@ template class elf::SymbolTableSection<ELF32BE>;
49704974template class elf ::SymbolTableSection<ELF64LE>;
49714975template class elf ::SymbolTableSection<ELF64BE>;
49724976
4973- template void elf::writeEhdr<ELF32LE>(uint8_t *Buf, Partition &Part);
4974- template void elf::writeEhdr<ELF32BE>(uint8_t *Buf, Partition &Part);
4975- template void elf::writeEhdr<ELF64LE>(uint8_t *Buf, Partition &Part);
4976- template void elf::writeEhdr<ELF64BE>(uint8_t *Buf, Partition &Part);
4977+ template void elf::writeEhdr<ELF32LE>(Ctx &, uint8_t *Buf, Partition &Part);
4978+ template void elf::writeEhdr<ELF32BE>(Ctx &, uint8_t *Buf, Partition &Part);
4979+ template void elf::writeEhdr<ELF64LE>(Ctx &, uint8_t *Buf, Partition &Part);
4980+ template void elf::writeEhdr<ELF64BE>(Ctx &, uint8_t *Buf, Partition &Part);
49774981
49784982template void elf::writePhdrs<ELF32LE>(uint8_t *Buf, Partition &Part);
49794983template void elf::writePhdrs<ELF32BE>(uint8_t *Buf, Partition &Part);
0 commit comments