-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
stage2: support dynamically linking musl libc #7406
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| const std = @import("std"); | ||
|
|
||
| const mem = std.mem; | ||
|
|
||
| const Symbol = struct { | ||
| name: []const u8, | ||
| section: []const u8, | ||
| kind: enum { | ||
| global, | ||
| weak, | ||
| }, | ||
| type: enum { | ||
| none, | ||
| function, | ||
| object, | ||
| }, | ||
| protected: bool, | ||
| }; | ||
|
|
||
| // Example usage: | ||
| // objdump --dynamic-syms /path/to/libc.so | ./gen_stubs > lib/libc/musl/libc.s | ||
| pub fn main() !void { | ||
| const stdin = std.io.getStdIn().reader(); | ||
| const stdout = std.io.getStdOut().writer(); | ||
|
|
||
| var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||
| defer arena.deinit(); | ||
| const ally = &arena.allocator; | ||
|
|
||
| var symbols = std.ArrayList(Symbol).init(ally); | ||
| var sections = std.ArrayList([]const u8).init(ally); | ||
|
|
||
| // This is many times larger than any line objdump produces should ever be | ||
| var buf: [4096]u8 = undefined; | ||
|
|
||
| // Sample input line: | ||
| // 00000000000241b0 g DF .text 000000000000001b copy_file_range | ||
| while (try stdin.readUntilDelimiterOrEof(&buf, '\n')) |line| { | ||
| // the lines we want all start with a 16 digit hex value | ||
| if (line.len < 16) continue; | ||
| _ = std.fmt.parseInt(u64, line[0..16], 16) catch continue; | ||
|
|
||
| // Ignore non-dynamic symbols | ||
| if (line[22] != 'D') continue; | ||
|
|
||
| const section = line[25 .. 25 + mem.indexOfAny(u8, line[25..], &std.ascii.spaces).?]; | ||
|
|
||
| // the last whitespace-separated column is the symbol name | ||
| const name = line[1 + mem.lastIndexOfAny(u8, line, &std.ascii.spaces).? ..]; | ||
|
|
||
| const symbol = Symbol{ | ||
| .name = try ally.dupe(u8, name), | ||
| .section = try ally.dupe(u8, section), | ||
|
|
||
| .kind = if (line[17] == 'g' and line[18] == ' ') | ||
| .global | ||
| else if (line[17] == ' ' and line[18] == 'w') | ||
| .weak | ||
| else | ||
| unreachable, | ||
|
|
||
| .type = switch (line[23]) { | ||
| 'F' => .function, | ||
| 'O' => .object, | ||
| ' ' => .none, | ||
| else => unreachable, | ||
| }, | ||
|
|
||
| .protected = mem.indexOf(u8, line, ".protected") != null, | ||
| }; | ||
|
|
||
| for (sections.items) |s| { | ||
| if (mem.eql(u8, s, symbol.section)) break; | ||
| } else { | ||
| try sections.append(symbol.section); | ||
| } | ||
|
|
||
| try symbols.append(symbol); | ||
| } | ||
|
|
||
| std.sort.sort(Symbol, symbols.items, {}, cmpSymbols); | ||
| std.sort.sort([]const u8, sections.items, {}, alphabetical); | ||
|
|
||
| for (sections.items) |section| { | ||
| try stdout.print("{s}\n", .{section}); | ||
|
|
||
| for (symbols.items) |symbol| { | ||
| if (!mem.eql(u8, symbol.section, section)) continue; | ||
|
|
||
| switch (symbol.kind) { | ||
| .global => try stdout.print(".globl {s}\n", .{symbol.name}), | ||
| .weak => try stdout.print(".weak {s}\n", .{symbol.name}), | ||
| } | ||
| switch (symbol.type) { | ||
| .function => try stdout.print(".type {s}, @function;\n", .{symbol.name}), | ||
| .object => try stdout.print(".type {s}, @object;\n", .{symbol.name}), | ||
| .none => {}, | ||
| } | ||
| if (symbol.protected) | ||
| try stdout.print(".protected {s}\n", .{symbol.name}); | ||
| try stdout.print("{s}:\n", .{symbol.name}); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn cmpSymbols(_: void, lhs: Symbol, rhs: Symbol) bool { | ||
| return alphabetical({}, lhs.name, rhs.name); | ||
| } | ||
|
|
||
| fn alphabetical(_: void, lhs: []const u8, rhs: []const u8) bool { | ||
| var i: usize = 0; | ||
| while (i < lhs.len and i < rhs.len) : (i += 1) { | ||
| if (lhs[i] == rhs[i]) continue; | ||
| return lhs[i] < rhs[i]; | ||
| } | ||
| return lhs.len < rhs.len; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to do some more filtering check out glibc's
abilist.awk(see here. Musl'slibc.sois quite tame if compared to glibc's one so most of that is not needed (yet?)/One thing that's wrong even in the glibc parsing scripts is the use of
.type N, @function;, that's ok for functions but not really ok for other symbol kinds (eg.environis marked asDOinstead ofDFin objdump's output).Since the symbol types "percolate" to the final object this may be a problem...
Here's the objdump's output of a simple program referencing
environ, the first part is when musl's libc is used, the second when the generatedlibc.sois used instead:Will this be a problem? In theory yes, every tool that checks the symbol type will be extremely confused by this.