-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
analyze: add inspect-elf verb to parse package metadata #21454
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
This file contains 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 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 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 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 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,128 @@ | ||
/* SPDX-License-Identifier: LGPL-2.1-or-later */ | ||
|
||
bluca marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include "analyze-elf.h" | ||
#include "elf-util.h" | ||
#include "errno-util.h" | ||
#include "fd-util.h" | ||
#include "format-table.h" | ||
#include "format-util.h" | ||
#include "json.h" | ||
#include "path-util.h" | ||
#include "strv.h" | ||
|
||
int analyze_elf(char **filenames, JsonFormatFlags json_flags) { | ||
char **filename; | ||
int r; | ||
|
||
STRV_FOREACH(filename, filenames) { | ||
_cleanup_(json_variant_unrefp) JsonVariant *package_metadata = NULL; | ||
_cleanup_(table_unrefp) Table *t = NULL; | ||
_cleanup_free_ char *abspath = NULL; | ||
_cleanup_close_ int fd = -1; | ||
|
||
r = path_make_absolute_cwd(*filename, &abspath); | ||
if (r < 0) | ||
return log_error_errno(r, "Could not make an absolute path out of \"%s\": %m", *filename); | ||
|
||
path_simplify(abspath); | ||
|
||
fd = RET_NERRNO(open(abspath, O_RDONLY|O_CLOEXEC)); | ||
if (fd < 0) | ||
return log_error_errno(fd, "Could not open \"%s\": %m", abspath); | ||
|
||
r = parse_elf_object(fd, abspath, /* fork_disable_dump= */false, NULL, &package_metadata); | ||
if (r < 0) | ||
return log_error_errno(r, "Parsing \"%s\" as ELF object failed: %m", abspath); | ||
|
||
t = table_new("", ""); | ||
if (!t) | ||
return log_oom(); | ||
|
||
r = table_set_align_percent(t, TABLE_HEADER_CELL(0), 100); | ||
if (r < 0) | ||
return table_log_add_error(r); | ||
|
||
r = table_add_many( | ||
t, | ||
TABLE_STRING, "path:", | ||
TABLE_STRING, abspath); | ||
if (r < 0) | ||
return table_log_add_error(r); | ||
|
||
if (package_metadata) { | ||
JsonVariant *module_json; | ||
const char *module_name; | ||
|
||
JSON_VARIANT_OBJECT_FOREACH(module_name, module_json, package_metadata) { | ||
const char *field_name; | ||
JsonVariant *field; | ||
|
||
/* The ELF type and architecture are added as top-level objects, | ||
* since they are only parsed for the file itself, but the packaging | ||
* metadata is parsed recursively in core files, so there might be | ||
* multiple modules. */ | ||
if (STR_IN_SET(module_name, "elfType", "elfArchitecture")) { | ||
_cleanup_free_ char *suffixed = NULL; | ||
|
||
suffixed = strjoin(module_name, ":"); | ||
if (!suffixed) | ||
return log_oom(); | ||
|
||
r = table_add_many( | ||
t, | ||
TABLE_STRING, suffixed, | ||
TABLE_STRING, json_variant_string(module_json)); | ||
if (r < 0) | ||
return table_log_add_error(r); | ||
|
||
continue; | ||
} | ||
|
||
/* path/elfType/elfArchitecture come first just once per file, | ||
* then we might have multiple modules, so add a separator between | ||
* them to make the output more readable. */ | ||
r = table_add_many(t, TABLE_EMPTY, TABLE_EMPTY); | ||
if (r < 0) | ||
return table_log_add_error(r); | ||
|
||
/* In case of core files the module name will be the executable, | ||
* but for binaries/libraries it's just the path, so don't print it | ||
* twice. */ | ||
if (!streq(abspath, module_name)) { | ||
r = table_add_many( | ||
t, | ||
TABLE_STRING, "module name:", | ||
TABLE_STRING, module_name); | ||
if (r < 0) | ||
return table_log_add_error(r); | ||
} | ||
|
||
JSON_VARIANT_OBJECT_FOREACH(field_name, field, module_json) | ||
if (json_variant_is_string(field)) { | ||
_cleanup_free_ char *suffixed = NULL; | ||
|
||
suffixed = strjoin(field_name, ":"); | ||
if (!suffixed) | ||
return log_oom(); | ||
|
||
r = table_add_many( | ||
t, | ||
TABLE_STRING, suffixed, | ||
TABLE_STRING, json_variant_string(field)); | ||
if (r < 0) | ||
return table_log_add_error(r); | ||
} | ||
} | ||
} | ||
if (json_flags & JSON_FORMAT_OFF) { | ||
(void) table_set_header(t, true); | ||
|
||
r = table_print(t, NULL); | ||
if (r < 0) | ||
return table_log_print_error(r); | ||
} else | ||
json_variant_dump(package_metadata, json_flags, stdout, NULL); | ||
} | ||
|
||
return 0; | ||
} |
This file contains 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,6 @@ | ||
/* SPDX-License-Identifier: LGPL-2.1-or-later */ | ||
#pragma once | ||
|
||
#include "json.h" | ||
|
||
int analyze_elf(char **filenames, JsonFormatFlags json_flags); |
This file contains 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 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
Oops, something went wrong.
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.
I think needs some annotation that an executable file should follow. But I don't know how to do this off the top of my head. Let's leave that for 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.
yeah, me neither, never really used zsh