diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp index c5d77142fda06..03976ff2d6882 100644 --- a/lld/MachO/SyntheticSections.cpp +++ b/lld/MachO/SyntheticSections.cpp @@ -47,7 +47,8 @@ InStruct macho::in; 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/SyntheticSections.h b/lld/MachO/SyntheticSections.h index 95a09a010e274..73d4c31534386 100644 --- a/lld/MachO/SyntheticSections.h +++ b/lld/MachO/SyntheticSections.h @@ -46,7 +46,7 @@ class SyntheticSection : public OutputSection { 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/Writer.cpp b/lld/MachO/Writer.cpp index 5520e65d881f2..443cd99b73f49 100644 --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -855,7 +855,7 @@ static void sortSegmentsAndSections() { } } -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/MachO/Writer.h b/lld/MachO/Writer.h index 56f6f7ae6fe76..2a6cc63d5b82d 100644 --- 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 @@ class LoadCommand { template void writeResult(); +NamePair maybeRenameSection(NamePair key); + void createSyntheticSections(); // Add bindings for symbols that need weak or non-lazy bindings. diff --git a/lld/test/MachO/rename.s b/lld/test/MachO/rename.s index 0b74ff1fdfd28..58144f05df79b 100644 --- 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_sect: _from_seg: .space 8 +## This is a synthetic section; make sure it gets renamed too. +.cstring + .space 8 + .text .global _main _main: