--- debugedit-4.4.6/debugedit.c 2006-06-09 14:17:06.000000000 +0000 +++ debugedit-4.4.6-r2/debugedit.c 2008-05-03 02:28:07.045354814 +0000 @@ -43,6 +43,8 @@ char *base_dir = NULL; char *dest_dir = NULL; char *list_file = NULL; +int print_files; +int read_only; int list_file_fd = -1; typedef unsigned int uint_32; @@ -567,13 +569,13 @@ char *p; size_t size; ssize_t ret; - if (base_dir) - p = s + strlen (base_dir); - else - p = s; if (list_file_fd != -1) { + if (base_dir) + p = s + strlen (base_dir); + else + p = s; size = strlen (p) + 1; while (size > 0) { @@ -584,6 +586,23 @@ p += ret; } } + if (print_files) + { + if (base_dir) + p = s + strlen (base_dir); + else + p = s; + size = strlen (p) + 1; + while (size > 0) + { + ret = write (STDOUT_FILENO, p, size); + if (ret == -1) + break; + write (STDOUT_FILENO, "\n", 1); + size -= ret; + p += ret; + } + } } free (s); @@ -1193,6 +1212,10 @@ "directory to rewrite base-dir into", NULL }, { "list-file", 'l', POPT_ARG_STRING, &list_file, 0, "file where to put list of source and header file names", NULL }, + { "print", 'p', POPT_ARG_NONE, &print_files, 0, + "print newline delimited list of file names to stdout", NULL }, + { "read-only", 'r', POPT_ARG_NONE, &read_only, 0, + "open read-only", NULL }, POPT_AUTOHELP { NULL, 0, 0, NULL, 0, NULL, NULL } }; @@ -1205,7 +1228,7 @@ int i; DSO *dso = NULL; - elf = elf_begin (fd, ELF_C_RDWR_MMAP, NULL); + elf = elf_begin (fd, read_only ? ELF_C_READ : ELF_C_RDWR_MMAP, NULL); if (elf == NULL) { error (0, 0, "cannot open ELF file: %s", elf_errmsg (-1)); @@ -1271,17 +1294,86 @@ return NULL; } +static int process(const char *file) +{ + DSO *dso; + int fd, i; + struct stat stat_buf; + + if (stat(file, &stat_buf) < 0) + { + fprintf (stderr, "Failed to open input file '%s': %s\n", file, strerror(errno)); + return -1; + } + + if (!read_only) { + /* Make sure we can read and write */ + chmod (file, stat_buf.st_mode | S_IRUSR | S_IWUSR); + } + + fd = open (file, read_only ? O_RDONLY : O_RDWR); + if (fd < 0) + { + fprintf (stderr, "Failed to open input file '%s': %s\n", file, strerror(errno)); + return -1; + } + + dso = fdopen_dso (fd, file); + if (dso == NULL) + goto error; + + for (i = 1; i < dso->ehdr.e_shnum; i++) + { + const char *name; + + switch (dso->shdr[i].sh_type) + { + case SHT_PROGBITS: + name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[i].sh_name); + /* TODO: Handle stabs */ +#if 0 + if (strcmp (name, ".stab") == 0) + edit_stabs (dso, i); +#endif + if (strcmp (name, ".debug_info") == 0) + edit_dwarf2 (dso); + + break; + default: + break; + } + } + + if (!read_only && elf_update (dso->elf, ELF_C_WRITE) < 0) + { + fprintf (stderr, "Failed to write file: %s\n", elf_errmsg (elf_errno())); + goto error; + } + if (elf_end (dso->elf) < 0) + { + fprintf (stderr, "elf_end failed: %s\n", elf_errmsg (elf_errno())); + goto error; + } + close (fd); + + if (!read_only) { + /* Restore old access rights */ + chmod (file, stat_buf.st_mode); + } + + return 0; + +error: + close(fd); + return -1; +} int main (int argc, char *argv[]) { - DSO *dso; - int fd, i; - const char *file; poptContext optCon; /* context for parsing command-line options */ int nextopt; const char **args; - struct stat stat_buf; char *p; optCon = poptGetContext("debugedit", argc, (const char **)argv, @@ -1300,12 +1392,17 @@ } args = poptGetArgs (optCon); - if (args == NULL || args[0] == NULL || args[1] != NULL) + if (args == NULL || args[0] == NULL) { poptPrintHelp(optCon, stdout, 0); exit (1); } + if (elf_version(EV_CURRENT) == EV_NONE) + { + fprintf (stderr, "library out of date\n"); + exit (1); + } if (dest_dir != NULL) { if (base_dir == NULL) @@ -1342,72 +1439,10 @@ { list_file_fd = open (list_file, O_WRONLY|O_CREAT|O_APPEND, 0644); } - - file = args[0]; - if (elf_version(EV_CURRENT) == EV_NONE) - { - fprintf (stderr, "library out of date\n"); - exit (1); - } + while (*args) + process(*args++); - if (stat(file, &stat_buf) < 0) - { - fprintf (stderr, "Failed to open input file '%s': %s\n", file, strerror(errno)); - exit (1); - } - - /* Make sure we can read and write */ - chmod (file, stat_buf.st_mode | S_IRUSR | S_IWUSR); - - fd = open (file, O_RDWR); - if (fd < 0) - { - fprintf (stderr, "Failed to open input file '%s': %s\n", file, strerror(errno)); - exit (1); - } - - dso = fdopen_dso (fd, file); - if (dso == NULL) - exit (1); - - for (i = 1; i < dso->ehdr.e_shnum; i++) - { - const char *name; - - switch (dso->shdr[i].sh_type) - { - case SHT_PROGBITS: - name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[i].sh_name); - /* TODO: Handle stabs */ -#if 0 - if (strcmp (name, ".stab") == 0) - edit_stabs (dso, i); -#endif - if (strcmp (name, ".debug_info") == 0) - edit_dwarf2 (dso); - - break; - default: - break; - } - } - - if (elf_update (dso->elf, ELF_C_WRITE) < 0) - { - fprintf (stderr, "Failed to write file: %s\n", elf_errmsg (elf_errno())); - exit (1); - } - if (elf_end (dso->elf) < 0) - { - fprintf (stderr, "elf_end failed: %s\n", elf_errmsg (elf_errno())); - exit (1); - } - close (fd); - - /* Restore old access rights */ - chmod (file, stat_buf.st_mode); - poptFreeContext (optCon); return 0;