From da92f2fb33c4e5fc570a51b0615e3d30a5b0bf8f Mon Sep 17 00:00:00 2001 From: Wael Yehia Date: Mon, 27 Feb 2023 23:26:52 -0500 Subject: [PATCH] [AIX][Driver] Implement -mxcoff-build-id option The -mxcoff-build-id=0xHEXSTRING option is an alternative to the --build-id=0xHEXSTRING linker option that is not currently available in the AIX linker. If HEXSTRING is an odd number of hex digits then a '0' character is prepended. The characters ':' and '-' are not allowed (unlike the GNU linker option). The given build-id will be saved in the string table of the loader section. A subsequent commit will teach the profile runtime to read and use the embedded id. Reviewed By: daltenty, qiongsiwu1, stephenpeckham Differential Revision: https://reviews.llvm.org/D146431 --- clang/include/clang/Driver/Options.td | 2 ++ clang/lib/Driver/ToolChains/AIX.cpp | 16 ++++++++++++++++ clang/test/Driver/xcoff-build-id.c | 14 ++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 clang/test/Driver/xcoff-build-id.c diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 2f81f60..5d2089e 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -3905,6 +3905,8 @@ defm zvector : BoolFOption<"zvector", def mzvector : Flag<["-"], "mzvector">, Alias; def mno_zvector : Flag<["-"], "mno-zvector">, Alias; +def mxcoff_build_id_EQ : Joined<["-"], "mxcoff-build-id=">, Group, MetaVarName<"<0xHEXSTRING>">, + HelpText<"On AIX, request creation of a build-id string, \"0xHEXSTRING\", in the string table of the loader section inside the linked binary">; def mignore_xcoff_visibility : Flag<["-"], "mignore-xcoff-visibility">, Group, HelpText<"Not emit the visibility attribute for asm in AIX OS or give all symbols 'unspecified' visibility in XCOFF object file">, Flags<[CC1Option]>; diff --git a/clang/lib/Driver/ToolChains/AIX.cpp b/clang/lib/Driver/ToolChains/AIX.cpp index 711e861..0ac261b 100644 --- a/clang/lib/Driver/ToolChains/AIX.cpp +++ b/clang/lib/Driver/ToolChains/AIX.cpp @@ -143,6 +143,22 @@ void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA, Args.hasArg(options::OPT_coverage)) CmdArgs.push_back("-bdbg:namedsects:ss"); + if (Arg *A = + Args.getLastArg(clang::driver::options::OPT_mxcoff_build_id_EQ)) { + StringRef BuildId = A->getValue(); + if (BuildId[0] != '0' || BuildId[1] != 'x' || + BuildId.find_if_not(llvm::isHexDigit, 2) != StringRef::npos) + ToolChain.getDriver().Diag(diag::err_drv_unsupported_option_argument) + << A->getSpelling() << BuildId; + else { + std::string LinkerFlag = "-bdbg:ldrinfo:xcoff_binary_id:0x"; + if (BuildId.size() % 2) // Prepend a 0 if odd number of digits. + LinkerFlag += "0"; + LinkerFlag += BuildId.drop_front(2).lower(); + CmdArgs.push_back(Args.MakeArgString(LinkerFlag)); + } + } + // Specify linker output file. assert((Output.isFilename() || Output.isNothing()) && "Invalid output."); if (Output.isFilename()) { diff --git a/clang/test/Driver/xcoff-build-id.c b/clang/test/Driver/xcoff-build-id.c new file mode 100644 index 0000000..5f12f5b --- /dev/null +++ b/clang/test/Driver/xcoff-build-id.c @@ -0,0 +1,14 @@ +// RUN: touch %t.o +// RUN: %clang --target=powerpc64-ibm-aix -### %t.o -mxcoff-build-id=0x12 2>&1 | FileCheck %s +// Test that: +// 1) our ldrinfo flag comes before any user specified ldrinfo flag; +// 2) upper case hex digits are converted to lower case; +// 3) a zero is added when odd number of digits is specified in the HEXSTRING. +// RUN: %clang --target=powerpc-ibm-aix -### %t.o -Wl,-bdbg:ldrinfo:FOO -mxcoff-build-id=0x011ffFF 2>&1 | FileCheck %s --check-prefix=OTHER + +// RUN: %clang --target=powerpc-ibm-aix -### %t.o -mxcoff-build-id=ff 2>&1 | FileCheck %s --check-prefix=BAD_INPUT +// RUN: %clang --target=powerpc-ibm-aix -### %t.o -mxcoff-build-id=0x0z 2>&1 | FileCheck %s --check-prefix=BAD_INPUT + +CHECK: "-bdbg:ldrinfo:xcoff_binary_id:0x12" +OTHER: "-bdbg:ldrinfo:xcoff_binary_id:0x0011ffff" {{.*}} "-bdbg:ldrinfo:FOO" +BAD_INPUT: clang: error: unsupported argument {{.*}} to option '-mxcoff-build-id=' -- 2.7.4