Skip to content

[LLD][COFF] Add support for the -defArm64Native argument #123850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lld/COFF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,9 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
setMachine(machine);
}
}

// Most of main arguments apply either to both or only to EC symbol table on
// ARM64X target.
SymbolTable &mainSymtab = ctx.hybridSymtab ? *ctx.hybridSymtab : ctx.symtab;

// Handle /nodefaultlib:<filename>
Expand Down Expand Up @@ -2291,6 +2294,12 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
if (auto *arg = args.getLastArg(OPT_deffile)) {
// parseModuleDefs mutates Config object.
mainSymtab.parseModuleDefs(arg->getValue());
if (ctx.hybridSymtab) {
// MSVC ignores the /defArm64Native argument on non-ARM64X targets.
// It is also ignored if the /def option is not specified.
if (auto *arg = args.getLastArg(OPT_defarm64native))
ctx.symtab.parseModuleDefs(arg->getValue());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: The naming of the variables constantly feels unintuitive to me here, where mainSymtab is the EC symbol table, while ctx.symtab (which similarly feels like the "main" one) is the native one.

We can't probably do much about it, because this is how the linker is supposed to be have, if mimicing link.exe, but we perhaps make it clearer. I think it would be good to have a comment on the definition of mainSymtab to really spell out what it is, and why.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, which symbol table is considered 'main' or 'primary' depends on the context. In the writer, the main one is ctx.symtab, as it’s directly referenced by the PE headers. Here, for lack of a better term, I meant the symbol table to which the main arguments should apply (though not all do yet). I'll add a comment for clarification.

}
}

// Handle generation of import library from a def file.
Expand Down
3 changes: 3 additions & 0 deletions lld/COFF/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ def incl : Joined<["/", "-", "/?", "-?"], "include:">,
def deffile : Joined<["/", "-", "/?", "-?"], "def:">,
HelpText<"Use module-definition file">;

def defarm64native
: P<"defarm64native",
"Use a module-definition file for the native view in a hybrid image.">;
def debug : F<"debug">, HelpText<"Embed a symbol table in the image">;
def debug_opt : P<"debug", "Embed a symbol table in the image with option">;
def debugtype : P<"debugtype", "Debug Info Options">;
Expand Down
23 changes: 23 additions & 0 deletions lld/test/COFF/arm64x-export.test
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,29 @@ EXPORTS-BOTH-NEXT: RVA: 0x3000
EXPORTS-BOTH-NEXT: }
EXPORTS-BOTH-NEXT: }

# Export using both the -def and -defarm64native arguments.

RUN: lld-link -machine:arm64x -dll -out:out-def-both.dll arm64ec-func.obj arm64-func.obj \
RUN: loadconfig-arm64.obj loadconfig-arm64ec.obj -def:func.def -defarm64native:func.def -noentry
RUN: llvm-objdump -d out-def-both.dll | FileCheck --check-prefix=DISASM-BOTH %s
RUN: llvm-readobj --headers --coff-exports out-def-both.dll | FileCheck --check-prefix=EXPORTS-BOTH %s

# -defarm64native is ignored if -def is not specified.

RUN: lld-link -machine:arm64x -dll -out:out-def-native.dll arm64ec-func.obj arm64-func.obj \
RUN: loadconfig-arm64.obj loadconfig-arm64ec.obj -defarm64native:func.def -noentry
RUN: llvm-readobj --headers --coff-exports out-def-native.dll | FileCheck --check-prefix=NO-EXPORT %s
NO-EXPORT: ExportTableRVA: 0x0
NO-EXPORT: ExportTableSize: 0x0
NO-EXPORT: HybridObject {
NO-EXPORT: ExportTableRVA: 0x0
NO-EXPORT: ExportTableSize: 0x0
NO-EXPORT: }

# -defarm64native is ignored on ARM64 target.

RUN: lld-link -machine:arm64 -dll -out:out-arm64-def.dll arm64-func.obj -defarm64native:invalid.def -def:func.def -noentry 2>&1 | count 0

# Export using both the native and EC .edata sections.

RUN: lld-link -machine:arm64x -dll -out:out-edata-both.dll arm64ec-func.obj arm64-func.obj \
Expand Down
Loading