summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authornorly <ny-git@enpas.org>2013-01-25 15:24:36 +0000
committernorly <ny-git@enpas.org>2013-02-11 01:24:36 +0000
commitd9eb4398773cbda1dc185f4cf7b1b0e4cb9fb135 (patch)
treea2421aa140b17db64df439f894fa892833e3ac96 /src/main.c
parent43e69328d849391abfed996214eed1dca49b3ac3 (diff)
Print ELF header/segments/sections
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..9791feb
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,74 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <getopt.h>
+#include <libelf.h>
+#include <gelf.h>
+
+#include "elfhandle.h"
+#include "options.h"
+#include "printing.h"
+
+
+int main(int argc, char **argv)
+{
+ CLIOpts opts = { 0 };
+ ELFHandles hIn = { 0 };
+ ELFHandles hOut = { 0 };
+ int exitval = EXIT_SUCCESS;
+
+ /* Is libelf alive and well? */
+ if (elf_version(EV_CURRENT) == EV_NONE) {
+ fprintf(stderr, "libelf init error: %s\n", elf_errmsg(-1));
+ }
+
+
+ /* Parse and validate user input */
+ parseOptions(&opts, argc, argv);
+
+
+ /* Open input/output files */
+ openElf(&hIn, opts.fnInput, ELF_C_READ);
+ if (!hIn.e) {
+ exitval = EXIT_FAILURE;
+ goto EXIT;
+ }
+
+ if (opts.fnOutput) {
+ openElf(&hOut, opts.fnOutput, ELF_C_WRITE);
+ if (!hOut.e) {
+ exitval = EXIT_FAILURE;
+ goto EXIT;
+ }
+ }
+
+
+ /* Now that we have a (hopefully) sane environment, execute commands */
+ if (opts.printHeader) {
+ printHeader(hIn.e);
+ }
+
+ if (opts.printSegments) {
+ printSegments(hIn.e);
+ }
+
+ if (opts.printSections) {
+ printSections(hIn.e);
+ }
+
+
+
+EXIT:
+ if (hOut.e) {
+ if (elf_update(hOut.e, ELF_C_WRITE) < 0) {
+ fprintf(stderr, "elf_update() failed: %s\n", elf_errmsg(-1));
+ }
+ closeElf(&hOut);
+ }
+
+ if (hIn.e) {
+ closeElf(&hIn);
+ }
+
+ return (exitval);
+}