Skip to content
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

add linker -wrap flag #15097

Merged
merged 2 commits into from
Apr 7, 2023
Merged

add linker -wrap flag #15097

merged 2 commits into from
Apr 7, 2023

Conversation

zachcheu
Copy link
Contributor

@zachcheu zachcheu commented Mar 28, 2023

Fixes: #15051

This passes -wrap through to the old.

Test

> cat wrap_example.c foo.c
#include <stdio.h>
extern int foo();
extern int __real_foo();

int __wrap_foo() {
    printf("wrap foo\n");
    return 0;
}

int __real_foo(){
    printf("real foo\n");
    return 0;
}

int main () {
    printf("foo:");foo();
    printf("wrapfoo:");__wrap_foo();
    printf("realfoo:");__real_foo();
    return 0;
}
#include <stdio.h>
int foo() {
    printf("foo\n");
    return 0;
}

> ./zig2 cc wrap_example.c foo.c  -Wl,-wrap,foo -o main

> ./main
foo:wrap foo
wrapfoo:wrap foo
realfoo:foo

Copy link
Contributor

@motiejus motiejus left a comment

Choose a reason for hiding this comment

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

cc @kubkon

This is causing annoyance in Uber's Go Monorepo. If you agree this is a simple-enough change, can we get it landed?

@motiejus
Copy link
Contributor

Also @zachcheu you may want to add Fixes: #15051 to the PR description, this will close #15051 automatically.

Copy link
Member

@kubkon kubkon left a comment

Choose a reason for hiding this comment

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

Please bump the Compilation.link_hash_implementation_version in

zig/src/Compilation.zig

Lines 2185 to 2189 in 28d6dd7

/// This is only observed at compile-time and used to emit a compile error
/// to remind the programmer to update multiple related pieces of code that
/// are in different locations. Bump this number when adding or deleting
/// anything from the link cache manifest.
pub const link_hash_implementation_version = 7;
and the wrap list is correctly put in cache in all relevant places.

Secondly, I see you've added -wrap flag which affects zig build-exe etc, however I don't see a matching addition to zig cc which would fall under `-Wl,"--wrap=symbol"' most likely. See

zig/src/main.zig

Line 1659 in 28d6dd7

.wl => {

src/main.zig Outdated
@@ -2263,6 +2267,14 @@ fn buildOutputType(
next_arg,
});
};
} else if (mem.eql(u8, arg, "-wrap") or
mem.eql(u8, arg, "--wrap"))
Copy link
Member

Choose a reason for hiding this comment

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

I don't see any reason in having --wrap here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Secondly, I see you've added -wrap flag which affects zig build-exe etc, however I don't see a matching addition to zig cc which would fall under `-Wl,"--wrap=symbol"' most likely.

I don't believe an explicit case -wrap case is necessary. The final else case takes care of flags that are split by comma

zig/src/main.zig

Line 1718 in 6f15eed

try linker_args.append(linker_arg);

-Wl,--wrap=symbol is taken care by

zig/src/main.zig

Line 1668 in 6f15eed

if (mem.indexOfScalar(u8, linker_arg, '=')) |equals_pos| {

Building from source captures both of these scenarios

> ./zig2 cc wrap_example.c foo.c  -Wl,-wrap,foo -o main

> ./main
foo:wrap foo
wrapfoo:wrap foo
realfoo:foo

> ./zig2 cc wrap_example.c foo.c  -Wl,-wrap=foo -o main

> ./main
foo:wrap foo
wrapfoo:wrap foo
realfoo:foo

I don't believe this flag is available for build-exe

> ./zig2 build-exe wrap_example.c foo.c  -Wl,-wrap=foo -o main
error: unrecognized parameter: '-Wl,-wrap=foo'

> ./zig2 build-exe wrap_example.c foo.c  -Wl,-wrap,foo -o main
error: unrecognized parameter: '-Wl,-wrap,foo'

Copy link
Member

Choose a reason for hiding this comment

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

Good catch about -Wl,-wrap being implicitly handled in the loop - I missed that.

Re the latter, zig build-exe does not understand -Wl flag and should handle an explicit -wrap flag.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this what you mean by supporting zig build-exe?

> ./zig2 build-exe -lc main.c foo.c -wrap foo

> ./main
foo:wrap foo
wrapfoo:wrap foo
realfoo:foo

src/main.zig Outdated Show resolved Hide resolved
* use a set instead of a list
* use of this flag currently requires LLD
* add documentation
* make it only a zig cc compatibility flag for now because I personally
  think this is an anti-feature.
@andrewrk andrewrk enabled auto-merge April 7, 2023 14:56
auto-merge was automatically disabled April 7, 2023 16:51

Head branch was pushed to by a user without write access

@zachcheu zachcheu requested a review from kristoff-it as a code owner April 7, 2023 16:51
@andrewrk
Copy link
Member

andrewrk commented Apr 7, 2023

hi @zachcheu, I'm sorry for confusing you - I added a commit to the branch and force pushed, and then set to auto-merge once the CI tests passed. It looks like you attempted to push some other work after that just now, but ran into git troubles.

I can take it from here. Are you OK with that?

@zachcheu
Copy link
Contributor Author

zachcheu commented Apr 7, 2023

No worries. Feel free to take it from here. Thanks!

@andrewrk andrewrk removed the request for review from kristoff-it April 7, 2023 21:09
@andrewrk andrewrk enabled auto-merge April 7, 2023 21:10
@andrewrk andrewrk merged commit 55a8b7e into ziglang:master Apr 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unsupported linker arg -wrap
4 participants