* Don't include libbfd.h outside of bfd, part 3
2016-07-16 13:10 Don't include libbfd.h outside of bfd, part 1 Alan Modra
2016-07-16 13:12 ` Don't include libbfd.h outside of bfd, part 2 Alan Modra
@ 2016-07-16 13:13 ` Alan Modra
2016-07-16 13:14 ` Don't include libbfd.h outside of bfd, part 5 Alan Modra
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Alan Modra @ 2016-07-16 13:13 UTC (permalink / raw)
To: binutils
Fix od-macho.c to use a leb128 function from binutils/dwarf.c rather
than one from bfd/libbfd.c.
binutils/
* elfcomm.h (HOST_WIDEST_INT): Move to..
* sysdep.h: ..here.
* od-macho.c: Don't include libbfd.h. Do include dwarf.h
(dump_dyld_info_rebase): Use read_leb128 rather than
read_unsigned_leb128.
(dump_dyld_info_bind, dump_dyld_info_export_1): Likewise.
(dump_segment_split_info): Likewise.
(dump_dyld_info): Rename vars to avoid shadowing dwarf.h enums.
(dump_load_command): Likewise.
diff --git a/binutils/elfcomm.h b/binutils/elfcomm.h
index 6ffd0c1..f9c43d1 100644
--- a/binutils/elfcomm.h
+++ b/binutils/elfcomm.h
@@ -29,13 +29,6 @@
void error (const char *, ...) ATTRIBUTE_PRINTF_1;
void warn (const char *, ...) ATTRIBUTE_PRINTF_1;
-#if defined HAVE_LONG_LONG && SIZEOF_LONG_LONG > SIZEOF_LONG
-/* We can't use any bfd types here since readelf may define BFD64 and
- objdump may not. */
-#define HOST_WIDEST_INT long long
-#else
-#define HOST_WIDEST_INT long
-#endif
typedef unsigned HOST_WIDEST_INT elf_vma;
extern void (*byte_put) (unsigned char *, elf_vma, int);
diff --git a/binutils/sysdep.h b/binutils/sysdep.h
index b4d8049..f28d9dc 100644
--- a/binutils/sysdep.h
+++ b/binutils/sysdep.h
@@ -187,4 +187,12 @@ size_t strnlen (const char *, size_t);
# endif
#endif
+#if defined HAVE_LONG_LONG && SIZEOF_LONG_LONG > SIZEOF_LONG
+/* We can't use any bfd types here since readelf may define BFD64 and
+ objdump may not. */
+#define HOST_WIDEST_INT long long
+#else
+#define HOST_WIDEST_INT long
+#endif
+
#endif /* _BIN_SYSDEP_H */
diff --git a/binutils/od-macho.c b/binutils/od-macho.c
index 0c86cad..d1a4e0f 100644
--- a/binutils/od-macho.c
+++ b/binutils/od-macho.c
@@ -26,8 +26,8 @@
#include "bfd.h"
#include "objdump.h"
#include "bucomm.h"
+#include "dwarf.h"
#include "bfdlink.h"
-#include "libbfd.h"
#include "mach-o.h"
#include "mach-o/external.h"
#include "mach-o/codesign.h"
@@ -688,13 +688,13 @@ dump_dyld_info_rebase (bfd *abfd, unsigned char *buf, unsigned int len,
bfd_mach_o_get_name (bfd_mach_o_dyld_rebase_type_name, imm));
break;
case BFD_MACH_O_REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
- leb = read_unsigned_leb128 (abfd, buf + i, &leblen);
+ leb = read_leb128 (buf + i, &leblen, 0, buf + len);
printf ("set segment: %u and offset: 0x%08x\n",
imm, (unsigned) leb);
i += leblen;
break;
case BFD_MACH_O_REBASE_OPCODE_ADD_ADDR_ULEB:
- leb = read_unsigned_leb128 (abfd, buf + i, &leblen);
+ leb = read_leb128 (buf + i, &leblen, 0, buf + len);
printf ("add addr uleb: 0x%08x\n", (unsigned) leb);
i += leblen;
break;
@@ -705,20 +705,20 @@ dump_dyld_info_rebase (bfd *abfd, unsigned char *buf, unsigned int len,
printf ("rebase imm times: %u\n", imm);
break;
case BFD_MACH_O_REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
- leb = read_unsigned_leb128 (abfd, buf + i, &leblen);
+ leb = read_leb128 (buf + i, &leblen, 0, buf + len);
printf ("rebase uleb times: %u\n", (unsigned) leb);
i += leblen;
break;
case BFD_MACH_O_REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
- leb = read_unsigned_leb128 (abfd, buf + i, &leblen);
+ leb = read_leb128 (buf + i, &leblen, 0, buf + len);
printf ("rebase add addr uleb: %u\n", (unsigned) leb);
i += leblen;
break;
case BFD_MACH_O_REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
- leb = read_unsigned_leb128 (abfd, buf + i, &leblen);
+ leb = read_leb128 (buf + i, &leblen, 0, buf + len);
printf ("rebase uleb times (%u)", (unsigned) leb);
i += leblen;
- leb = read_unsigned_leb128 (abfd, buf + i, &leblen);
+ leb = read_leb128 (buf + i, &leblen, 0, buf + len);
printf (" skipping uleb (%u)\n", (unsigned) leb);
i += leblen;
break;
@@ -755,7 +755,7 @@ dump_dyld_info_bind (bfd *abfd, unsigned char *buf, unsigned int len,
printf ("set dylib ordinal imm: %u\n", imm);
break;
case BFD_MACH_O_BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
- leb = read_unsigned_leb128 (abfd, buf + i, &leblen);
+ leb = read_leb128 (buf + i, &leblen, 0, buf + len);
printf ("set dylib ordinal uleb: %u\n", imm);
i += leblen;
break;
@@ -778,19 +778,19 @@ dump_dyld_info_bind (bfd *abfd, unsigned char *buf, unsigned int len,
case BFD_MACH_O_BIND_OPCODE_SET_ADDEND_SLEB:
{
bfd_signed_vma svma;
- svma = read_unsigned_leb128 (abfd, buf + i, &leblen);
+ svma = read_leb128 (buf + i, &leblen, 0, buf + len);
printf ("set addend sleb: 0x%08x\n", (unsigned) svma);
i += leblen;
}
break;
case BFD_MACH_O_BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
- leb = read_unsigned_leb128 (abfd, buf + i, &leblen);
+ leb = read_leb128 (buf + i, &leblen, 0, buf + len);
printf ("set segment: %u and offset: 0x%08x\n",
imm, (unsigned) leb);
i += leblen;
break;
case BFD_MACH_O_BIND_OPCODE_ADD_ADDR_ULEB:
- leb = read_unsigned_leb128 (abfd, buf + i, &leblen);
+ leb = read_leb128 (buf + i, &leblen, 0, buf + len);
printf ("add addr uleb: 0x%08x\n", (unsigned) leb);
i += leblen;
break;
@@ -798,7 +798,7 @@ dump_dyld_info_bind (bfd *abfd, unsigned char *buf, unsigned int len,
printf ("do bind\n");
break;
case BFD_MACH_O_BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
- leb = read_unsigned_leb128 (abfd, buf + i, &leblen);
+ leb = read_leb128 (buf + i, &leblen, 0, buf + len);
printf ("do bind add addr uleb: 0x%08x\n", (unsigned) leb);
i += leblen;
break;
@@ -806,10 +806,10 @@ dump_dyld_info_bind (bfd *abfd, unsigned char *buf, unsigned int len,
printf ("do bind add addr imm scaled: %u\n", imm * ptrsize);
break;
case BFD_MACH_O_BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
- leb = read_unsigned_leb128 (abfd, buf + i, &leblen);
+ leb = read_leb128 (buf + i, &leblen, 0, buf + len);
printf ("do bind uleb times (%u)", (unsigned) leb);
i += leblen;
- leb = read_unsigned_leb128 (abfd, buf + i, &leblen);
+ leb = read_leb128 (buf + i, &leblen, 0, buf + len);
printf (" skipping uleb (%u)\n", (unsigned) leb);
i += leblen;
break;
@@ -837,7 +837,7 @@ dump_dyld_info_export_1 (bfd *abfd, unsigned char *buf, unsigned int len,
unsigned int child_count;
unsigned int i;
- size = read_unsigned_leb128 (abfd, buf + off, &leblen);
+ size = read_leb128 (buf + off, &leblen, 0, buf + len);
off += leblen;
if (size != 0)
@@ -845,7 +845,7 @@ dump_dyld_info_export_1 (bfd *abfd, unsigned char *buf, unsigned int len,
bfd_vma flags;
struct export_info_data *d;
- flags = read_unsigned_leb128 (abfd, buf + off, &leblen);
+ flags = read_leb128 (buf + off, &leblen, 0, buf + len);
off += leblen;
fputs (" ", stdout);
@@ -868,7 +868,7 @@ dump_dyld_info_export_1 (bfd *abfd, unsigned char *buf, unsigned int len,
{
bfd_vma lib;
- lib = read_unsigned_leb128 (abfd, buf + off, &leblen);
+ lib = read_leb128 (buf + off, &leblen, 0, buf + len);
off += leblen;
fputs (" [reexport] ", stdout);
@@ -890,12 +890,12 @@ dump_dyld_info_export_1 (bfd *abfd, unsigned char *buf, unsigned int len,
bfd_vma offset;
bfd_vma resolv = 0;
- offset = read_unsigned_leb128 (abfd, buf + off, &leblen);
+ offset = read_leb128 (buf + off, &leblen, 0, buf + len);
off += leblen;
if (flags & BFD_MACH_O_EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER)
{
- resolv = read_unsigned_leb128 (abfd, buf + off, &leblen);
+ resolv = read_leb128 (buf + off, &leblen, 0, buf + len);
off += leblen;
}
@@ -908,7 +908,7 @@ dump_dyld_info_export_1 (bfd *abfd, unsigned char *buf, unsigned int len,
}
}
- child_count = read_unsigned_leb128 (abfd, buf + off, &leblen);
+ child_count = read_leb128 (buf + off, &leblen, 0, buf + len);
off += leblen;
for (i = 0; i < child_count; i++)
@@ -922,7 +922,7 @@ dump_dyld_info_export_1 (bfd *abfd, unsigned char *buf, unsigned int len,
off += strlen ((const char *)buf + off) + 1;
- sub_off = read_unsigned_leb128 (abfd, buf + off, &leblen);
+ sub_off = read_leb128 (buf + off, &leblen, 0, buf + len);
off += leblen;
dump_dyld_info_export_1 (abfd, buf, len, sub_off, &sub_data, base);
@@ -946,49 +946,49 @@ static void
dump_dyld_info (bfd *abfd, bfd_mach_o_load_command *cmd,
bfd_boolean verbose)
{
- bfd_mach_o_dyld_info_command *info = &cmd->command.dyld_info;
+ bfd_mach_o_dyld_info_command *dinfo = &cmd->command.dyld_info;
printf (" rebase: off: 0x%08x size: %-8u (endoff: 0x%08x)\n",
- info->rebase_off, info->rebase_size,
- info->rebase_off + info->rebase_size);
+ dinfo->rebase_off, dinfo->rebase_size,
+ dinfo->rebase_off + dinfo->rebase_size);
printf (" bind: off: 0x%08x size: %-8u (endoff: 0x%08x)\n",
- info->bind_off, info->bind_size,
- info->bind_off + info->bind_size);
+ dinfo->bind_off, dinfo->bind_size,
+ dinfo->bind_off + dinfo->bind_size);
printf (" weak bind: off: 0x%08x size: %-8u (endoff: 0x%08x)\n",
- info->weak_bind_off, info->weak_bind_size,
- info->weak_bind_off + info->weak_bind_size);
+ dinfo->weak_bind_off, dinfo->weak_bind_size,
+ dinfo->weak_bind_off + dinfo->weak_bind_size);
printf (" lazy bind: off: 0x%08x size: %-8u (endoff: 0x%08x)\n",
- info->lazy_bind_off, info->lazy_bind_size,
- info->lazy_bind_off + info->lazy_bind_size);
+ dinfo->lazy_bind_off, dinfo->lazy_bind_size,
+ dinfo->lazy_bind_off + dinfo->lazy_bind_size);
printf (" export: off: 0x%08x size: %-8u (endoff: 0x%08x)\n",
- info->export_off, info->export_size,
- info->export_off + info->export_size);
+ dinfo->export_off, dinfo->export_size,
+ dinfo->export_off + dinfo->export_size);
if (!verbose)
return;
printf (" rebase:\n");
- if (!load_and_dump (abfd, info->rebase_off, info->rebase_size,
+ if (!load_and_dump (abfd, dinfo->rebase_off, dinfo->rebase_size,
dump_dyld_info_rebase))
non_fatal (_("cannot read rebase dyld info"));
printf (" bind:\n");
- if (!load_and_dump (abfd, info->bind_off, info->bind_size,
+ if (!load_and_dump (abfd, dinfo->bind_off, dinfo->bind_size,
dump_dyld_info_bind))
non_fatal (_("cannot read bind dyld info"));
printf (" weak bind:\n");
- if (!load_and_dump (abfd, info->weak_bind_off, info->weak_bind_size,
+ if (!load_and_dump (abfd, dinfo->weak_bind_off, dinfo->weak_bind_size,
dump_dyld_info_bind))
non_fatal (_("cannot read weak bind dyld info"));
printf (" lazy bind:\n");
- if (!load_and_dump (abfd, info->lazy_bind_off, info->lazy_bind_size,
+ if (!load_and_dump (abfd, dinfo->lazy_bind_off, dinfo->lazy_bind_size,
dump_dyld_info_bind))
non_fatal (_("cannot read lazy bind dyld info"));
printf (" exported symbols:\n");
- if (!load_and_dump (abfd, info->export_off, info->export_size,
+ if (!load_and_dump (abfd, dinfo->export_off, dinfo->export_size,
dump_dyld_info_export))
non_fatal (_("cannot read export symbols dyld info"));
}
@@ -1287,7 +1287,7 @@ dump_segment_split_info (bfd *abfd, bfd_mach_o_linkedit_command *cmd)
}
for (p = buf + 1; *p != 0; p += len)
{
- addr += read_unsigned_leb128 (abfd, p, &len);
+ addr += read_leb128 (p, &len, 0, buf + cmd->datasize);
fputs (" ", stdout);
bfd_printf_vma (abfd, addr);
putchar ('\n');
@@ -1556,8 +1556,8 @@ dump_load_command (bfd *abfd, bfd_mach_o_load_command *cmd,
case BFD_MACH_O_LC_SUB_CLIENT:
case BFD_MACH_O_LC_RPATH:
{
- bfd_mach_o_str_command *str = &cmd->command.str;
- printf (" %s\n", str->str);
+ bfd_mach_o_str_command *strc = &cmd->command.str;
+ printf (" %s\n", strc->str);
break;
}
case BFD_MACH_O_LC_THREAD:
--
Alan Modra
Australia Development Lab, IBM
^ permalink raw reply [flat|nested] 6+ messages in thread
* Don't include libbfd.h outside of bfd, part 5
2016-07-16 13:10 Don't include libbfd.h outside of bfd, part 1 Alan Modra
2016-07-16 13:12 ` Don't include libbfd.h outside of bfd, part 2 Alan Modra
2016-07-16 13:13 ` Don't include libbfd.h outside of bfd, part 3 Alan Modra
@ 2016-07-16 13:14 ` Alan Modra
2016-07-16 13:14 ` Don't include libbfd.h outside of bfd, part 4 Alan Modra
2016-07-16 13:16 ` Don't include libbfd.h outside of bfd, part 6 Alan Modra
4 siblings, 0 replies; 6+ messages in thread
From: Alan Modra @ 2016-07-16 13:14 UTC (permalink / raw)
To: binutils
A rewrite of the code in bucomm.c supporting objdump -i, to use
bfd_iterate_over_targets rather than accessing bfd_target_vector
directly. Calculates widest arch string rather than using an
out of date constant. Stashes info from the first display of valid
target/arch combinations for use in second tabular display.
binutils/
* bucomm.c: Don't include libbfd.h.
(endian_string, display_target_list): Delete forward declaration.
(display_info_table, display_target_tables): Likewise.
(LONGEST_ARCH): Delete.
(struct display_target): New.
(do_display_target): New function.
(display_target_list, display_info): Rewrite functions.
(display_info_table): Delete.
(do_info_size, do_info_header, do_info_row): New functions.
diff --git a/binutils/bucomm.c b/binutils/bucomm.c
index e719cd3..22bf24a 100644
--- a/binutils/bucomm.c
+++ b/binutils/bucomm.c
@@ -25,7 +25,6 @@
#include "bfd.h"
#include "libiberty.h"
#include "filenames.h"
-#include "libbfd.h"
#include <time.h> /* ctime, maybe time_t */
#include <assert.h>
@@ -36,11 +35,6 @@
typedef long time_t;
#endif
#endif
-
-static const char * endian_string (enum bfd_endian);
-static int display_target_list (void);
-static int display_info_table (int, int);
-static int display_target_tables (void);
\f
/* Error reporting. */
@@ -216,9 +210,6 @@ list_supported_architectures (const char *name, FILE *f)
free (arches);
}
\f
-/* The length of the longest architecture name + 1. */
-#define LONGEST_ARCH sizeof ("powerpc:common")
-
static const char *
endian_string (enum bfd_endian endian)
{
@@ -230,184 +221,205 @@ endian_string (enum bfd_endian endian)
}
}
+/* Data passed to do_display_target and other target iterators. */
+
+struct display_target {
+ /* Temp file. */
+ char *filename;
+ /* Return status. */
+ int error;
+ /* Number of targets. */
+ int count;
+ /* Size of info in bytes. */
+ size_t alloc;
+ /* Per-target info. */
+ struct {
+ /* Target name. */
+ const char *name;
+ /* Non-zero if target/arch combination supported. */
+ unsigned char arch[bfd_arch_last - bfd_arch_obscure - 1];
+ } *info;
+};
+
/* List the targets that BFD is configured to support, each followed
- by its endianness and the architectures it supports. */
+ by its endianness and the architectures it supports. Also build
+ info about target/archs. */
static int
-display_target_list (void)
+do_display_target (const bfd_target *targ, void *data)
{
- char *dummy_name;
- int t;
- int ret = 1;
+ struct display_target *param = (struct display_target *) data;
+ bfd *abfd;
+ size_t amt;
- dummy_name = make_temp_file (NULL);
- for (t = 0; bfd_target_vector[t]; t++)
+ param->count += 1;
+ amt = param->count * sizeof (*param->info);
+ if (param->alloc < amt)
{
- const bfd_target *p = bfd_target_vector[t];
- bfd *abfd = bfd_openw (dummy_name, p->name);
- int a;
-
- printf (_("%s\n (header %s, data %s)\n"), p->name,
- endian_string (p->header_byteorder),
- endian_string (p->byteorder));
+ size_t size = ((param->count < 64 ? 64 : param->count)
+ * sizeof (*param->info) * 2);
+ param->info = xrealloc (param->info, size);
+ memset ((char *) param->info + param->alloc, 0, size - param->alloc);
+ param->alloc = size;
+ }
+ param->info[param->count - 1].name = targ->name;
- if (abfd == NULL)
- {
- bfd_nonfatal (dummy_name);
- ret = 0;
- continue;
- }
+ printf (_("%s\n (header %s, data %s)\n"), targ->name,
+ endian_string (targ->header_byteorder),
+ endian_string (targ->byteorder));
- if (! bfd_set_format (abfd, bfd_object))
+ abfd = bfd_openw (param->filename, targ->name);
+ if (abfd == NULL)
+ {
+ bfd_nonfatal (param->filename);
+ param->error = 1;
+ }
+ else if (!bfd_set_format (abfd, bfd_object))
+ {
+ if (bfd_get_error () != bfd_error_invalid_operation)
{
- if (bfd_get_error () != bfd_error_invalid_operation)
- {
- bfd_nonfatal (p->name);
- ret = 0;
- }
- bfd_close_all_done (abfd);
- continue;
+ bfd_nonfatal (targ->name);
+ param->error = 1;
}
+ }
+ else
+ {
+ enum bfd_architecture a;
for (a = bfd_arch_obscure + 1; a < bfd_arch_last; a++)
- if (bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
- printf (" %s\n",
- bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
- bfd_close_all_done (abfd);
+ if (bfd_set_arch_mach (abfd, a, 0))
+ {
+ printf (" %s\n", bfd_printable_arch_mach (a, 0));
+ param->info[param->count - 1].arch[a - bfd_arch_obscure - 1] = 1;
+ }
}
- unlink (dummy_name);
- free (dummy_name);
+ if (abfd != NULL)
+ bfd_close_all_done (abfd);
- return ret;
+ return param->error;
+}
+
+static void
+display_target_list (struct display_target *arg)
+{
+ arg->filename = make_temp_file (NULL);
+ arg->error = 0;
+ arg->count = 0;
+ arg->alloc = 0;
+ arg->info = NULL;
+
+ bfd_iterate_over_targets (do_display_target, arg);
+
+ unlink (arg->filename);
+ free (arg->filename);
}
-/* Print a table showing which architectures are supported for entries
- FIRST through LAST-1 of bfd_target_vector (targets across,
- architectures down). */
+/* Calculate how many targets we can print across the page. */
static int
-display_info_table (int first, int last)
+do_info_size (int targ, int width, const struct display_target *arg)
{
- int t;
- int ret = 1;
- char *dummy_name;
- int a;
-
- /* Print heading of target names. */
- printf ("\n%*s", (int) LONGEST_ARCH, " ");
- for (t = first; t < last && bfd_target_vector[t]; t++)
- printf ("%s ", bfd_target_vector[t]->name);
- putchar ('\n');
-
- dummy_name = make_temp_file (NULL);
- for (a = bfd_arch_obscure + 1; a < bfd_arch_last; a++)
- if (strcmp (bfd_printable_arch_mach ((enum bfd_architecture) a, 0),
- "UNKNOWN!") != 0)
- {
- printf ("%*s ", (int) LONGEST_ARCH - 1,
- bfd_printable_arch_mach ((enum bfd_architecture) a, 0));
- for (t = first; t < last && bfd_target_vector[t]; t++)
- {
- const bfd_target *p = bfd_target_vector[t];
- bfd_boolean ok = TRUE;
- bfd *abfd = bfd_openw (dummy_name, p->name);
-
- if (abfd == NULL)
- {
- bfd_nonfatal (p->name);
- ret = 0;
- ok = FALSE;
- }
-
- if (ok)
- {
- if (! bfd_set_format (abfd, bfd_object))
- {
- if (bfd_get_error () != bfd_error_invalid_operation)
- {
- bfd_nonfatal (p->name);
- ret = 0;
- }
- ok = FALSE;
- }
- }
-
- if (ok)
- {
- if (! bfd_set_arch_mach (abfd, (enum bfd_architecture) a, 0))
- ok = FALSE;
- }
-
- if (ok)
- printf ("%s ", p->name);
- else
- {
- int l = strlen (p->name);
- while (l--)
- putchar ('-');
- putchar (' ');
- }
- if (abfd != NULL)
- bfd_close_all_done (abfd);
- }
- putchar ('\n');
- }
- unlink (dummy_name);
- free (dummy_name);
+ while (targ < arg->count)
+ {
+ width -= strlen (arg->info[targ].name) + 1;
+ if (width < 0)
+ return targ;
+ ++targ;
+ }
+ return targ;
+}
- return ret;
+/* Print header of target names. */
+
+static void
+do_info_header (int targ, int stop_targ, const struct display_target *arg)
+{
+ while (targ != stop_targ)
+ printf ("%s ", arg->info[targ++].name);
+}
+
+/* Print a table row. */
+
+static void
+do_info_row (int targ, int stop_targ, enum bfd_architecture a,
+ const struct display_target *arg)
+{
+ while (targ != stop_targ)
+ {
+ if (arg->info[targ].arch[a - bfd_arch_obscure - 1])
+ fputs (arg->info[targ].name, stdout);
+ else
+ {
+ int l = strlen (arg->info[targ].name);
+ while (l--)
+ putchar ('-');
+ }
+ ++targ;
+ if (targ != stop_targ)
+ putchar (' ');
+ }
}
/* Print tables of all the target-architecture combinations that
BFD has been configured to support. */
-static int
-display_target_tables (void)
+static void
+display_target_tables (const struct display_target *arg)
{
- int t;
- int columns;
- int ret = 1;
- char *colum;
-
- columns = 0;
- colum = getenv ("COLUMNS");
- if (colum != NULL)
- columns = atoi (colum);
- if (columns == 0)
- columns = 80;
-
- t = 0;
- while (bfd_target_vector[t] != NULL)
+ const char *columns;
+ int width, start_targ, stop_targ;
+ enum bfd_architecture arch;
+ int longest_arch = 0;
+
+ for (arch = bfd_arch_obscure + 1; arch < bfd_arch_last; arch++)
{
- int oldt = t, wid;
+ const char *s = bfd_printable_arch_mach (arch, 0);
+ int len = strlen (s);
+ if (len > longest_arch)
+ longest_arch = len;
+ }
- wid = LONGEST_ARCH + strlen (bfd_target_vector[t]->name) + 1;
- ++t;
- while (wid < columns && bfd_target_vector[t] != NULL)
- {
- int newwid;
+ width = 0;
+ columns = getenv ("COLUMNS");
+ if (columns != NULL)
+ width = atoi (columns);
+ if (width == 0)
+ width = 80;
- newwid = wid + strlen (bfd_target_vector[t]->name) + 1;
- if (newwid >= columns)
- break;
- wid = newwid;
- ++t;
+ for (start_targ = 0; start_targ < arg->count; start_targ = stop_targ)
+ {
+ stop_targ = do_info_size (start_targ, width - longest_arch - 1, arg);
+
+ printf ("\n%*s", longest_arch + 1, " ");
+ do_info_header (start_targ, stop_targ, arg);
+ putchar ('\n');
+
+ for (arch = bfd_arch_obscure + 1; arch < bfd_arch_last; arch++)
+ {
+ if (strcmp (bfd_printable_arch_mach (arch, 0), "UNKNOWN!") != 0)
+ {
+ printf ("%*s ", longest_arch,
+ bfd_printable_arch_mach (arch, 0));
+
+ do_info_row (start_targ, stop_targ, arch, arg);
+ putchar ('\n');
+ }
}
- if (! display_info_table (oldt, t))
- ret = 0;
}
-
- return ret;
}
int
display_info (void)
{
+ struct display_target arg;
+
printf (_("BFD header file version %s\n"), BFD_VERSION_STRING);
- if (! display_target_list () || ! display_target_tables ())
- return 1;
- else
- return 0;
+
+ display_target_list (&arg);
+ if (!arg.error)
+ display_target_tables (&arg);
+
+ return arg.error;
}
\f
/* Display the archive header for an element as if it were an ls -l listing:
--
Alan Modra
Australia Development Lab, IBM
^ permalink raw reply [flat|nested] 6+ messages in thread
* Don't include libbfd.h outside of bfd, part 6
2016-07-16 13:10 Don't include libbfd.h outside of bfd, part 1 Alan Modra
` (3 preceding siblings ...)
2016-07-16 13:14 ` Don't include libbfd.h outside of bfd, part 4 Alan Modra
@ 2016-07-16 13:16 ` Alan Modra
4 siblings, 0 replies; 6+ messages in thread
From: Alan Modra @ 2016-07-16 13:16 UTC (permalink / raw)
To: binutils
Some messing with plugin code in order to not need arelt_size in
ld code. File descriptor handling in ld/plugin.c is tidied too,
simply duping the open fd rather than opening the file again.
bfd/
* elflink.c: Include plugin-api.h.
* plugin.c (bfd_plugin_open_input): New function, extracted from..
(try_claim): ..here.
* plugin.h: Don't include bfd.h.
(bfd_plugin_open_input): Declare.
binutils/
* ar.c: Include plugin-api.h.
* nm.c: Likewise.
ld/
* plugin.c: Don't include libbfd.h. Include plugin-api.h
before bfd/plugin.h.
(plugin_object_p): Use bfd_plugin_open_input.
diff --git a/bfd/elflink.c b/bfd/elflink.c
index b2a814f..a994b83 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -29,6 +29,7 @@
#include "libiberty.h"
#include "objalloc.h"
#if BFD_SUPPORTS_PLUGINS
+#include "plugin-api.h"
#include "plugin.h"
#endif
diff --git a/bfd/plugin.c b/bfd/plugin.c
index c66d95e..3931d27 100644
--- a/bfd/plugin.c
+++ b/bfd/plugin.c
@@ -158,49 +158,50 @@ bfd_plugin_set_program_name (const char *program_name)
plugin_program_name = program_name;
}
-static int
-try_claim (bfd *abfd)
+int
+bfd_plugin_open_input (bfd *ibfd, struct ld_plugin_input_file *file)
{
- int claimed = 0;
- struct ld_plugin_input_file file;
bfd *iobfd;
- file.name = abfd->filename;
-
- if (abfd->my_archive && !bfd_is_thin_archive (abfd->my_archive))
- {
- iobfd = abfd->my_archive;
- file.offset = abfd->origin;
- file.filesize = arelt_size (abfd);
- }
- else
- {
- iobfd = abfd;
- file.offset = 0;
- file.filesize = 0;
- }
+ iobfd = ibfd;
+ if (ibfd->my_archive && !bfd_is_thin_archive (ibfd->my_archive))
+ iobfd = ibfd->my_archive;
+ file->name = iobfd->filename;
if (!iobfd->iostream && !bfd_open_file (iobfd))
return 0;
- file.fd = fileno ((FILE *) iobfd->iostream);
+ file->fd = fileno ((FILE *) iobfd->iostream);
- if (!abfd->my_archive || bfd_is_thin_archive (abfd->my_archive))
+ if (iobfd == ibfd)
{
struct stat stat_buf;
- if (fstat (file.fd, &stat_buf))
+ if (fstat (file->fd, &stat_buf))
return 0;
- file.filesize = stat_buf.st_size;
+ file->offset = 0;
+ file->filesize = stat_buf.st_size;
+ }
+ else
+ {
+ file->offset = ibfd->origin;
+ file->filesize = arelt_size (ibfd);
}
+ return 1;
+}
+
+static int
+try_claim (bfd *abfd)
+{
+ int claimed = 0;
+ struct ld_plugin_input_file file;
+ if (!bfd_plugin_open_input (abfd, &file))
+ return 0;
file.handle = abfd;
- off_t cur_offset = lseek(file.fd, 0, SEEK_CUR);
+ off_t cur_offset = lseek (file.fd, 0, SEEK_CUR);
claim_file (&file, &claimed);
- lseek(file.fd, cur_offset, SEEK_SET);
- if (!claimed)
- return 0;
-
- return 1;
+ lseek (file.fd, cur_offset, SEEK_SET);
+ return claimed;
}
static int
diff --git a/bfd/plugin.h b/bfd/plugin.h
index 529f8c1..0867122 100644
--- a/bfd/plugin.h
+++ b/bfd/plugin.h
@@ -21,9 +21,8 @@
#ifndef _PLUGIN_H_
#define _PLUGIN_H_
-#include "bfd.h"
-
void bfd_plugin_set_program_name (const char *);
+int bfd_plugin_open_input (bfd *, struct ld_plugin_input_file *);
void bfd_plugin_set_plugin (const char *);
bfd_boolean bfd_plugin_target_p (const bfd_target *);
bfd_boolean bfd_plugin_specified_p (void);
diff --git a/binutils/ar.c b/binutils/ar.c
index 1337710..ba0d581 100644
--- a/binutils/ar.c
+++ b/binutils/ar.c
@@ -33,6 +33,7 @@
#include "arsup.h"
#include "filenames.h"
#include "binemul.h"
+#include "plugin-api.h"
#include "plugin.h"
#ifdef __GO32___
diff --git a/binutils/nm.c b/binutils/nm.c
index 5ca4d34..766564d 100644
--- a/binutils/nm.c
+++ b/binutils/nm.c
@@ -36,6 +36,7 @@
#include "coff/internal.h"
#include "libcoff.h"
#include "bucomm.h"
+#include "plugin-api.h"
#include "plugin.h"
/* When sorting by size, we use this structure to hold the size and a
diff --git a/ld/plugin.c b/ld/plugin.c
index 924b59c..ffa0dd3 100644
--- a/ld/plugin.c
+++ b/ld/plugin.c
@@ -21,7 +21,6 @@
#include "sysdep.h"
#include "libiberty.h"
#include "bfd.h"
-#include "libbfd.h"
#include "bfdlink.h"
#include "bfdver.h"
#include "ld.h"
@@ -30,9 +29,9 @@
#include "ldexp.h"
#include "ldlang.h"
#include "ldfile.h"
+#include "plugin-api.h"
#include "../bfd/plugin.h"
#include "plugin.h"
-#include "plugin-api.h"
#include "elf-bfd.h"
#if HAVE_MMAP
# include <sys/mman.h>
@@ -1083,12 +1082,8 @@ plugin_object_p (bfd *ibfd)
{
int claimed;
plugin_input_file_t *input;
- off_t offset, filesize;
struct ld_plugin_input_file file;
bfd *abfd;
- bfd_boolean inarchive;
- const char *name;
- int fd;
/* Don't try the dummy object file. */
if ((ibfd->flags & BFD_PLUGIN) != 0)
@@ -1102,14 +1097,6 @@ plugin_object_p (bfd *ibfd)
return NULL;
}
- inarchive = (ibfd->my_archive != NULL
- && !bfd_is_thin_archive (ibfd->my_archive));
- name = inarchive ? ibfd->my_archive->filename : ibfd->filename;
- fd = open (name, O_RDONLY | O_BINARY);
-
- if (fd < 0)
- return NULL;
-
/* We create a dummy BFD, initially empty, to house whatever symbols
the plugin may want to add. */
abfd = plugin_get_ir_dummy_bfd (ibfd->filename, ibfd);
@@ -1119,39 +1106,31 @@ plugin_object_p (bfd *ibfd)
einfo (_("%P%F: plugin failed to allocate memory for input: %s\n"),
bfd_get_error ());
- if (inarchive)
- {
- /* Offset and filesize must refer to the individual archive
- member, not the whole file, and must exclude the header.
- Fortunately for us, that is how the data is stored in the
- origin field of the bfd and in the arelt_data. */
- offset = ibfd->origin;
- filesize = arelt_size (ibfd);
- }
- else
- {
- offset = 0;
- filesize = lseek (fd, 0, SEEK_END);
+ if (!bfd_plugin_open_input (ibfd, &file))
+ return NULL;
+ if (file.name == ibfd->filename)
+ {
/* We must copy filename attached to ibfd if it is not an archive
member since it may be freed by bfd_close below. */
- name = plugin_strdup (abfd, name);
+ file.name = plugin_strdup (abfd, file.name);
}
- file.name = name;
- file.offset = offset;
- file.filesize = filesize;
- file.fd = fd;
file.handle = input;
+ /* The plugin API expects that the file descriptor won't be closed
+ and reused as done by the bfd file cache. So dup one. */
+ file.fd = dup (file.fd);
+ if (file.fd < 0)
+ return NULL;
input->abfd = abfd;
input->view_buffer.addr = NULL;
input->view_buffer.filesize = 0;
input->view_buffer.offset = 0;
- input->fd = fd;
+ input->fd = file.fd;
input->use_mmap = FALSE;
- input->offset = offset;
- input->filesize = filesize;
+ input->offset = file.offset;
+ input->filesize = file.filesize;
input->name = plugin_strdup (abfd, ibfd->filename);
claimed = 0;
@@ -1160,7 +1139,7 @@ plugin_object_p (bfd *ibfd)
einfo (_("%P%F: %s: plugin reported error claiming file\n"),
plugin_error_plugin ());
- if (input->fd != -1 && ! bfd_plugin_target_p (ibfd->xvec))
+ if (input->fd != -1 && !bfd_plugin_target_p (ibfd->xvec))
{
/* FIXME: fd belongs to us, not the plugin. GCC plugin, which
doesn't need fd after plugin_call_claim_file, doesn't use
@@ -1170,7 +1149,7 @@ plugin_object_p (bfd *ibfd)
release_input_file after it is done, uses BFD plugin target
vector. This scheme doesn't work when a plugin needs fd and
doesn't use BFD plugin target vector neither. */
- close (fd);
+ close (input->fd);
input->fd = -1;
}
--
Alan Modra
Australia Development Lab, IBM
^ permalink raw reply [flat|nested] 6+ messages in thread