summaryrefslogtreecommitdiff
path: root/cmake/elfinfo/elfinfo.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cmake/elfinfo/elfinfo.cpp')
-rw-r--r--cmake/elfinfo/elfinfo.cpp32
1 files changed, 22 insertions, 10 deletions
diff --git a/cmake/elfinfo/elfinfo.cpp b/cmake/elfinfo/elfinfo.cpp
index 05a847a..65fae85 100644
--- a/cmake/elfinfo/elfinfo.cpp
+++ b/cmake/elfinfo/elfinfo.cpp
@@ -12,6 +12,8 @@
#include <inttypes.h>
#include <elf.h>
+#include "ld.h"
+
using std::vector;
enum class SectionType {
@@ -49,25 +51,27 @@ struct Section {
vector<Section> sections;
-char *filename = NULL;
+char *filename = nullptr;
+
+char *ld_filename = nullptr;
char *program;
__attribute__((noreturn))
-void usage(const char *reason = NULL) {
- if (reason != NULL) {
+void usage(const char *reason = nullptr) {
+ if (reason) {
fprintf(stderr, "%s\n", reason);
}
- fprintf(stderr, "usage: %s -f file [-t start:size] [-d start:size]\n", program);
+ fprintf(stderr, "usage: %s -f file [-l ld] [-t start:size] [-d start:size]\n", program);
fprintf(stderr, " -t/-d/-b: add text/data section\n");
- fprintf(stderr, "At least one section has to be specified\n");
+ fprintf(stderr, "At least one section or a ld file has to be specified\n");
exit(EX_USAGE);
}
void parse_start_size(char *input, Elf64_Addr &start, Elf64_Xword &size) {
char *str_size = strchr(input, ':');
- if (str_size == NULL) {
+ if (!str_size) {
usage("bad section specification, missing ':'");
}
@@ -115,7 +119,7 @@ bool debug = false;
void parse_args(int argc, char **argv) {
int c;
- while ((c = getopt(argc, argv, "Df:t:d:")) != -1) {
+ while ((c = getopt(argc, argv, "Df:l:t:d:")) != -1) {
switch (c) {
case 'D':
debug = true;
@@ -132,6 +136,9 @@ void parse_args(int argc, char **argv) {
case 'f':
filename = optarg;
break;
+ case 'l':
+ ld_filename = optarg;
+ break;
case '?':
if (optopt == 'c')
errx(EX_USAGE, "Option -%c requires an argument.\n", optopt);
@@ -142,7 +149,7 @@ void parse_args(int argc, char **argv) {
}
}
- if (filename == NULL || sections.empty()) {
+ if (!filename || (sections.empty() && !ld_filename)) {
usage();
}
}
@@ -165,6 +172,11 @@ int main(int argc, char **argv) {
program = argv[0];
parse_args(argc, argv);
+ if (ld_filename) {
+ ld_file file = ld_file_loader::load(ld_filename);
+ // TODO: load sections
+ }
+
if (elf_version(EV_CURRENT) == EV_NONE)
errx(EX_SOFTWARE, "ELF library initialization failed: %s", elf_errmsg(-1));
@@ -244,12 +256,12 @@ int main(int argc, char **argv) {
}
printf("Size by sections\n");
- printf("Type Start End Size Used\n");
+ printf("Type Start End Size Used\n");
std::for_each(sections.begin(), sections.end(), [&](Section &s) {
char size[100];
to_iso(s.size, size);
int used_pct = (int) (double(s.used) / double(s.size) * 100.0);
- printf("%4s %08" PRIx64 " %08" PRIx64 " %5s %6" PRId64 " %3d%%\n", to_str(s.type), s.start, s.end, size, s.used,
+ printf("%4s %08" PRIx64 " %08" PRIx64 " %5s %8" PRId64 " %3d%%\n", to_str(s.type), s.start, s.end, size, s.used,
used_pct);
});