diff --git a/lld/MachO/SyntheticSections.h b/lld/MachO/SyntheticSections.h --- a/lld/MachO/SyntheticSections.h +++ b/lld/MachO/SyntheticSections.h @@ -46,7 +46,7 @@ return sec->kind() == SyntheticKind; } - const StringRef segname; + StringRef segname; // This fake InputSection makes it easier for us to write code that applies // generically to both user inputs and synthetics. InputSection *isec; diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp --- a/lld/MachO/SyntheticSections.cpp +++ b/lld/MachO/SyntheticSections.cpp @@ -47,7 +47,8 @@ std::vector macho::syntheticSections; SyntheticSection::SyntheticSection(const char *segname, const char *name) - : OutputSection(SyntheticKind, name), segname(segname) { + : OutputSection(SyntheticKind, name) { + std::tie(this->segname, this->name) = maybeRenameSection({segname, name}); isec = make(segname, name); isec->parent = this; syntheticSections.push_back(this); diff --git a/lld/MachO/Writer.h b/lld/MachO/Writer.h --- a/lld/MachO/Writer.h +++ b/lld/MachO/Writer.h @@ -9,6 +9,8 @@ #ifndef LLD_MACHO_WRITER_H #define LLD_MACHO_WRITER_H +#include "Config.h" + #include namespace lld { @@ -27,6 +29,8 @@ template void writeResult(); +NamePair maybeRenameSection(NamePair key); + void createSyntheticSections(); // Add bindings for symbols that need weak or non-lazy bindings. diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -855,7 +855,7 @@ } } -static NamePair maybeRenameSection(NamePair key) { +NamePair macho::maybeRenameSection(NamePair key) { auto newNames = config->sectionRenameMap.find(key); if (newNames != config->sectionRenameMap.end()) return newNames->second; diff --git a/lld/test/MachO/rename.s b/lld/test/MachO/rename.s --- a/lld/test/MachO/rename.s +++ b/lld/test/MachO/rename.s @@ -30,6 +30,7 @@ # RUN: %lld \ # RUN: -rename_section __FROM_SECT __from_sect __TO_SECT __to_sect \ # RUN: -rename_segment __FROM_SEG __TO_SEG \ +# RUN: -rename_section __TEXT __cstring __RODATA __cstring \ # RUN: -o %t %t.o # RUN: llvm-objdump --macho --all-headers %t | FileCheck %s @@ -37,14 +38,14 @@ # CHECK-NEXT: sectname __text # CHECK-NEXT: segname __TEXT # CHECK: {{^}}Section{{$}} -# CHECK-NOT: sectname __from_sect # CHECK-NEXT: sectname __to_sect -# CHECK-NOT: segname __FROM_SECT # CHECK-NEXT: segname __TO_SECT # CHECK: {{^}}Section{{$}} # CHECK-NEXT: sectname __from_seg -# CHECK-NOT: segname __FROM_SEG # CHECK-NEXT: segname __TO_SEG +# CHECK: {{^}}Section{{$}} +# CHECK-NEXT: sectname __cstring +# CHECK-NEXT: segname __RODATA .section __FROM_SECT,__from_sect .global _from_sect @@ -56,6 +57,10 @@ _from_seg: .space 8 +## This is a synthetic section; make sure it gets renamed too. +.cstring + .space 8 + .text .global _main _main: