From a6298fb160633578f5c4c1e739044274ba8fe520 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Fri, 28 Jan 2022 18:46:51 -0800 Subject: [PATCH] [lld-macho] Add support for -add_empty_section This is a ld64 option equivalent to `-sectcreate seg sect /dev/null` that's useful for creating sections like the RESTRICT section. Differential Revision: https://reviews.llvm.org/D117749 --- lld/MachO/Driver.cpp | 6 ++++++ lld/MachO/Options.td | 4 ++++ lld/test/MachO/sectcreate.s | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp index e4c9f4d..f0cddab 100644 --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -1484,6 +1484,12 @@ bool macho::link(ArrayRef argsArr, llvm::raw_ostream &stdoutOS, inputFiles.insert(make(*buffer, segName, sectName)); } + for (const Arg *arg : args.filtered(OPT_add_empty_section)) { + StringRef segName = arg->getValue(0); + StringRef sectName = arg->getValue(1); + inputFiles.insert(make(MemoryBufferRef(), segName, sectName)); + } + gatherInputSections(); if (config->callGraphProfileSort) extractCallGraphProfile(); diff --git a/lld/MachO/Options.td b/lld/MachO/Options.td index 3d1d976..ab79aa7 100644 --- a/lld/MachO/Options.td +++ b/lld/MachO/Options.td @@ -252,6 +252,10 @@ def segcreate : MultiArg<["-"], "segcreate", 3>, Alias, HelpText<"Alias for -sectcreate">, Group; +def add_empty_section : MultiArg<["-"], "add_empty_section", 2>, + MetaVarName<"
">, + HelpText<"Create an empty
in ">, + Group; def filelist : Separate<["-"], "filelist">, MetaVarName<"">, HelpText<"Read names of files to link from ">, diff --git a/lld/test/MachO/sectcreate.s b/lld/test/MachO/sectcreate.s index 03847e1..919c553 100644 --- a/lld/test/MachO/sectcreate.s +++ b/lld/test/MachO/sectcreate.s @@ -7,6 +7,7 @@ # RUN: -sectcreate SEG SEC1 %t1 \ # RUN: -segcreate SEG SEC2 %t3 \ # RUN: -sectcreate SEG SEC1 %t2 \ +# RUN: -add_empty_section __DATA __data \ # RUN: -o %t %t.o # RUN: llvm-objdump -s %t | FileCheck %s @@ -16,10 +17,17 @@ # RUN: -sectcreate SEG SEC1 %t1 \ # RUN: -segcreate SEG SEC2 %t3 \ # RUN: -sectcreate SEG SEC1 %t2 \ +# RUN: -add_empty_section SEG SEC1 \ # RUN: -o %t %t.o # RUN: llvm-objdump -s %t | FileCheck --check-prefix=STRIPPED %s # RUN: llvm-readobj --sections %t | FileCheck --check-prefix=STRIPPEDSEC %s +# RUN: %lld -add_empty_section foo bar -o %t %t.o +# RUN: llvm-readobj --sections %t | FileCheck --check-prefix=EMPTYSECTION %s + +# RUN: %lld -sectcreate SEG SEC1 %t1 -add_empty_section SEG SEC1 -o %t %t.o +# RUN: llvm-readobj --sections %t | FileCheck --check-prefix=CREATEDANDEMPTY %s + # CHECK: Contents of section __TEXT,__text: # CHECK: Contents of section __DATA,__data: # CHECK: my string!. @@ -40,6 +48,16 @@ # STRIPPEDSEC-NOT: NoDeadStrip +# EMPTYSECTION: Name: bar +# EMPTYSECTION: Segment: foo +# EMPTYSECTION: Size: 0x0 +# EMPTYSECTION-NOT: Name: + +# CREATEDANDEMPTY: Name: SEC1 +# CREATEDANDEMPTY: Segment: SEG +# CREATEDANDEMPTY: Size: 0x10 +# CREATEDANDEMPTY-NOT: Name: + .text .global _main _main: -- 2.7.4