Index: lld/ELF/Driver.cpp =================================================================== --- lld/ELF/Driver.cpp +++ lld/ELF/Driver.cpp @@ -856,8 +856,30 @@ return ret; } +// Return false iff the first line of symbol ordering file is "# +// apply_if_output_is: ", and `basename ` does not equal to +// `basename config->outputFile`. +static bool shouldApplySymbolOrderingFile(MemoryBufferRef mb) { + StringRef buf = mb.getBuffer(); + if (buf.empty() || buf[0] != '#') + return true; + size_t pos = buf.find_first_of('\n'); + if (pos == StringRef::npos) + return true; + // "line" starts from the first non-space character after "#". + StringRef line = buf.substr(1, pos).trim(); + if (line.consume_front("apply_if_output_is:")) { + // Remove any possible spaces betwee "apply_if_output_is:" and "". + StringRef filename = line.ltrim(); + return path::filename(filename) == path::filename(config->outputFile); + } + return true; +} + // Parse the symbol ordering file and warn for any duplicate entries. static std::vector getSymbolOrderingFile(MemoryBufferRef mb) { + if (!shouldApplySymbolOrderingFile(mb)) + return {}; SetVector names; for (StringRef s : args::getLines(mb)) if (!names.insert(s) && config->warnSymbolOrdering) Index: lld/ELF/Options.td =================================================================== --- lld/ELF/Options.td +++ lld/ELF/Options.td @@ -376,8 +376,13 @@ def strip_debug: F<"strip-debug">, HelpText<"Strip debugging information">; -defm symbol_ordering_file: - Eq<"symbol-ordering-file", "Layout sections to place symbols in the order specified by symbol ordering file">; +defm symbol_ordering_file + : Eq<"symbol-ordering-file", "Layout sections to place symbols in the " + "order specified by symbol ordering file, " + "an optional \"# apply_if_output_is: " + "\" can be specified at the " + "beginning to mean this file is only to be " + "used when linking ">; defm sysroot: Eq<"sysroot", "Set the system root">; Index: lld/test/ELF/symbol-ordering-file-apply-if-output-is.s =================================================================== --- /dev/null +++ lld/test/ELF/symbol-ordering-file-apply-if-output-is.s @@ -0,0 +1,38 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o +# RUN: echo "_foo2" > %t_order.txt +# RUN: echo "_foo1" >> %t_order.txt + +# RUN: ld.lld --symbol-ordering-file %t_order.txt %t.o -o %t2.out +# RUN: llvm-objdump -s %t2.out| FileCheck %s --check-prefix=ORDER_FILE + +# ORDER_FILE: Contents of section .foo: +# ORDER_FILE-NEXT: 2211 + +# RUN: echo "# apply_if_output_is: dummy_file_name_no_match" > %t_order.txt +# RUN: echo "_foo2" >> %t_order.txt +# RUN: echo "_foo1" >> %t_order.txt + +# RUN: ld.lld --symbol-ordering-file %t_order.txt %t.o -o %t2.out +# RUN: llvm-objdump -s %t2.out| FileCheck %s --check-prefix=ORDER_FILE_WITH_APPLY + +# ORDER_FILE_WITH_APPLY: Contents of section .foo: +# ORDER_FILE_WITH_APPLY-NEXT: 1122 + +# RUN: echo "# apply_if_output_is: %t2.out" > %t_order.txt +# RUN: echo "_foo2" >> %t_order.txt +# RUN: echo "_foo1" >> %t_order.txt + +# RUN: ld.lld --symbol-ordering-file %t_order.txt %t.o -o %t2.out +# RUN: llvm-objdump -s %t2.out| FileCheck %s --check-prefix=ORDER_FILE_WITH_APPLY2 + +# ORDER_FILE_WITH_APPLY2: Contents of section .foo: +# ORDER_FILE_WITH_APPLY2-NEXT: 2211 + +.section .foo,"ax",@progbits,unique,1 +_foo1: + .byte 0x11 + +.section .foo,"ax",@progbits,unique,2 +_foo2: + .byte 0x22