objcopy add-symbol uninitialised struct

* objcopy.c: Formatting, whitespace throughout.
	(copy_main): Init newsym->othersym.
	(parse_symflags): Make len a size_t.  Adjust uses.
This commit is contained in:
Alan Modra
2016-05-31 20:21:09 +09:30
parent c12969f8b5
commit f7433f011e
2 changed files with 119 additions and 110 deletions

View File

@ -1,3 +1,9 @@
2016-05-31 Alan Modra <amodra@gmail.com>
* objcopy.c: Formatting, whitespace throughout.
(copy_main): Init newsym->othersym.
(parse_symflags): Make len a size_t. Adjust uses.
2016-05-25 H.J. Lu <hongjiu.lu@intel.com> 2016-05-25 H.J. Lu <hongjiu.lu@intel.com>
PR binutils/14625 PR binutils/14625

View File

@ -715,8 +715,8 @@ parse_flags (const char *s)
} }
if (0) ; if (0) ;
#define PARSE_FLAG(fname,fval) \ #define PARSE_FLAG(fname,fval) \
else if (strncasecmp (fname, s, len) == 0) ret |= fval else if (strncasecmp (fname, s, len) == 0) ret |= fval
PARSE_FLAG ("alloc", SEC_ALLOC); PARSE_FLAG ("alloc", SEC_ALLOC);
PARSE_FLAG ("load", SEC_LOAD); PARSE_FLAG ("load", SEC_LOAD);
PARSE_FLAG ("noload", SEC_NEVER_LOAD); PARSE_FLAG ("noload", SEC_NEVER_LOAD);
@ -757,7 +757,7 @@ parse_symflags (const char *s, char **other)
{ {
flagword ret; flagword ret;
const char *snext; const char *snext;
int len; size_t len;
ret = BSF_NO_FLAGS; ret = BSF_NO_FLAGS;
@ -765,19 +765,21 @@ parse_symflags (const char *s, char **other)
{ {
snext = strchr (s, ','); snext = strchr (s, ',');
if (snext == NULL) if (snext == NULL)
len = strlen (s); len = strlen (s);
else else
{ {
len = snext - s; len = snext - s;
++snext; ++snext;
} }
#define PARSE_FLAG(fname,fval) \ #define PARSE_FLAG(fname, fval) \
else if (len == (int) sizeof fname - 1 && strncasecmp (fname, s, len) == 0) \ else if (len == sizeof fname - 1 \
&& strncasecmp (fname, s, len) == 0) \
ret |= fval ret |= fval
#define PARSE_OTHER(fname,fval) \ #define PARSE_OTHER(fname, fval) \
else if (len >= (int) sizeof fname && strncasecmp (fname, s, sizeof fname - 1) == 0) \ else if (len >= sizeof fname \
&& strncasecmp (fname, s, sizeof fname - 1) == 0) \
fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1) fval = xstrndup (s + sizeof fname - 1, len - sizeof fname + 1)
if (0) ; if (0) ;
@ -809,9 +811,9 @@ parse_symflags (const char *s, char **other)
copy[len] = '\0'; copy[len] = '\0';
non_fatal (_("unrecognized symbol flag `%s'"), copy); non_fatal (_("unrecognized symbol flag `%s'"), copy);
fatal (_("supported flags: %s"), fatal (_("supported flags: %s"),
"local, global, export, debug, function, weak, section, " "local, global, export, debug, function, weak, section, "
"constructor, warning, indirect, file, object, synthetic, " "constructor, warning, indirect, file, object, synthetic, "
"indirect-function, unique-object, before=<othersym>"); "indirect-function, unique-object, before=<othersym>");
} }
s = snext; s = snext;
@ -1160,12 +1162,12 @@ is_update_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
struct section_add *pupdate; struct section_add *pupdate;
for (pupdate = update_sections; for (pupdate = update_sections;
pupdate != NULL; pupdate != NULL;
pupdate = pupdate->next) pupdate = pupdate->next)
{ {
if (strcmp (sec->name, pupdate->name) == 0) if (strcmp (sec->name, pupdate->name) == 0)
return TRUE; return TRUE;
} }
} }
return FALSE; return FALSE;
@ -1190,8 +1192,8 @@ is_strip_section_1 (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
fatal (_("error: section %s matches both remove and copy options"), fatal (_("error: section %s matches both remove and copy options"),
bfd_get_section_name (abfd, sec)); bfd_get_section_name (abfd, sec));
if (p && is_update_section (abfd, sec)) if (p && is_update_section (abfd, sec))
fatal (_("error: section %s matches both update and remove options"), fatal (_("error: section %s matches both update and remove options"),
bfd_get_section_name (abfd, sec)); bfd_get_section_name (abfd, sec));
if (p != NULL) if (p != NULL)
return TRUE; return TRUE;
@ -1333,9 +1335,9 @@ need_sym_before (struct addsym_node **node, const char *sym)
static asymbol * static asymbol *
create_new_symbol (struct addsym_node *ptr, bfd *obfd) create_new_symbol (struct addsym_node *ptr, bfd *obfd)
{ {
asymbol *sym = bfd_make_empty_symbol(obfd); asymbol *sym = bfd_make_empty_symbol (obfd);
bfd_asymbol_name(sym) = ptr->symdef; bfd_asymbol_name (sym) = ptr->symdef;
sym->value = ptr->symval; sym->value = ptr->symval;
sym->flags = ptr->flags; sym->flags = ptr->flags;
if (ptr->section) if (ptr->section)
@ -1346,7 +1348,7 @@ create_new_symbol (struct addsym_node *ptr, bfd *obfd)
sym->section = sec; sym->section = sec;
} }
else else
sym->section = bfd_abs_section_ptr; sym->section = bfd_abs_section_ptr;
return sym; return sym;
} }
@ -1411,12 +1413,12 @@ filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
/* Short circuit for change_leading_char if we can do it in-place. */ /* Short circuit for change_leading_char if we can do it in-place. */
if (rem_leading_char && add_leading_char && !prefix_symbols_string) if (rem_leading_char && add_leading_char && !prefix_symbols_string)
{ {
name[0] = bfd_get_symbol_leading_char (obfd); name[0] = bfd_get_symbol_leading_char (obfd);
bfd_asymbol_name (sym) = name; bfd_asymbol_name (sym) = name;
rem_leading_char = FALSE; rem_leading_char = FALSE;
add_leading_char = FALSE; add_leading_char = FALSE;
} }
/* Remove leading char. */ /* Remove leading char. */
if (rem_leading_char) if (rem_leading_char)
@ -1424,23 +1426,23 @@ filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
/* Add new leading char and/or prefix. */ /* Add new leading char and/or prefix. */
if (add_leading_char || prefix_symbols_string) if (add_leading_char || prefix_symbols_string)
{ {
char *n, *ptr; char *n, *ptr;
ptr = n = (char *) xmalloc (1 + strlen (prefix_symbols_string) ptr = n = (char *) xmalloc (1 + strlen (prefix_symbols_string)
+ strlen (name) + 1); + strlen (name) + 1);
if (add_leading_char) if (add_leading_char)
*ptr++ = bfd_get_symbol_leading_char (obfd); *ptr++ = bfd_get_symbol_leading_char (obfd);
if (prefix_symbols_string) if (prefix_symbols_string)
{ {
strcpy (ptr, prefix_symbols_string); strcpy (ptr, prefix_symbols_string);
ptr += strlen (prefix_symbols_string); ptr += strlen (prefix_symbols_string);
} }
strcpy (ptr, name); strcpy (ptr, name);
bfd_asymbol_name (sym) = n; bfd_asymbol_name (sym) = n;
name = n; name = n;
} }
if (strip_symbols == STRIP_ALL) if (strip_symbols == STRIP_ALL)
@ -1684,7 +1686,7 @@ add_redefine_syms_file (const char *filename)
if ((c == '\r' && (c = getc (file)) == '\n') if ((c == '\r' && (c = getc (file)) == '\n')
|| c == '\n' || c == EOF) || c == '\n' || c == EOF)
{ {
end_of_line: end_of_line:
/* Append the redefinition to the list. */ /* Append the redefinition to the list. */
if (buf[0] != '\0') if (buf[0] != '\0')
redefine_list_append (filename, &buf[0], &buf[outsym_off]); redefine_list_append (filename, &buf[0], &buf[outsym_off]);
@ -1699,7 +1701,7 @@ add_redefine_syms_file (const char *filename)
} }
else else
fatal (_("%s:%d: garbage found at end of line"), filename, lineno); fatal (_("%s:%d: garbage found at end of line"), filename, lineno);
comment: comment:
if (len != 0 && (outsym_off == 0 || outsym_off == len)) if (len != 0 && (outsym_off == 0 || outsym_off == len))
fatal (_("%s:%d: missing new symbol name"), filename, lineno); fatal (_("%s:%d: missing new symbol name"), filename, lineno);
buf[len++] = '\0'; buf[len++] = '\0';
@ -2035,15 +2037,15 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
if (bfd_get_section_by_name (obfd, padd->name)) if (bfd_get_section_by_name (obfd, padd->name))
{ {
bfd_nonfatal_message (NULL, obfd, NULL, bfd_nonfatal_message (NULL, obfd, NULL,
_("can't add section '%s'"), padd->name); _("can't add section '%s'"), padd->name);
return FALSE; return FALSE;
} }
else else
{ {
/* We use LINKER_CREATED here so that the backend hooks /* We use LINKER_CREATED here so that the backend hooks
will create any special section type information, will create any special section type information,
instead of presuming we know what we're doing merely instead of presuming we know what we're doing merely
because we set the flags. */ because we set the flags. */
padd->section = bfd_make_section_with_flags padd->section = bfd_make_section_with_flags
(obfd, padd->name, flags | SEC_LINKER_CREATED); (obfd, padd->name, flags | SEC_LINKER_CREATED);
if (padd->section == NULL) if (padd->section == NULL)
@ -2406,14 +2408,14 @@ copy_object (bfd *ibfd, bfd *obfd, const bfd_arch_info_type *input_arch)
struct section_add *pupdate; struct section_add *pupdate;
for (pupdate = update_sections; for (pupdate = update_sections;
pupdate != NULL; pupdate != NULL;
pupdate = pupdate->next) pupdate = pupdate->next)
{ {
asection *osec; asection *osec;
osec = pupdate->section->output_section; osec = pupdate->section->output_section;
if (! bfd_set_section_contents (obfd, osec, pupdate->contents, if (! bfd_set_section_contents (obfd, osec, pupdate->contents,
0, pupdate->size)) 0, pupdate->size))
{ {
bfd_nonfatal_message (NULL, obfd, osec, NULL); bfd_nonfatal_message (NULL, obfd, osec, NULL);
return FALSE; return FALSE;
@ -2535,7 +2537,7 @@ copy_archive (bfd *ibfd, bfd *obfd, const char *output_target,
/* Make a temp directory to hold the contents. */ /* Make a temp directory to hold the contents. */
dir = make_tempdir (bfd_get_filename (obfd)); dir = make_tempdir (bfd_get_filename (obfd));
if (dir == NULL) if (dir == NULL)
fatal (_("cannot create tempdir for archive copying (error: %s)"), fatal (_("cannot create tempdir for archive copying (error: %s)"),
strerror (errno)); strerror (errno));
if (strip_symbols == STRIP_ALL) if (strip_symbols == STRIP_ALL)
@ -2802,7 +2804,7 @@ copy_file (const char *input_filename, const char *output_filename,
bfd *obfd; bfd *obfd;
/* bfd_get_target does not return the correct value until /* bfd_get_target does not return the correct value until
bfd_check_format succeeds. */ bfd_check_format succeeds. */
if (output_target == NULL) if (output_target == NULL)
{ {
output_target = bfd_get_target (ibfd); output_target = bfd_get_target (ibfd);
@ -2829,7 +2831,7 @@ copy_file (const char *input_filename, const char *output_filename,
do_copy: do_copy:
/* bfd_get_target does not return the correct value until /* bfd_get_target does not return the correct value until
bfd_check_format succeeds. */ bfd_check_format succeeds. */
if (output_target == NULL) if (output_target == NULL)
output_target = bfd_get_target (ibfd); output_target = bfd_get_target (ibfd);
@ -3024,7 +3026,7 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC)); flags = p->flags | (flags & (SEC_HAS_CONTENTS | SEC_RELOC));
else if (strip_symbols == STRIP_NONDEBUG else if (strip_symbols == STRIP_NONDEBUG
&& (flags & (SEC_ALLOC | SEC_GROUP)) != 0 && (flags & (SEC_ALLOC | SEC_GROUP)) != 0
&& !is_nondebug_keep_contents_section (ibfd, isection)) && !is_nondebug_keep_contents_section (ibfd, isection))
{ {
flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP); flags &= ~(SEC_HAS_CONTENTS | SEC_LOAD | SEC_GROUP);
if (obfd->xvec->flavour == bfd_target_elf_flavour) if (obfd->xvec->flavour == bfd_target_elf_flavour)
@ -3141,7 +3143,7 @@ setup_section (bfd *ibfd, sec_ptr isection, void *obfdarg)
/* All went well. */ /* All went well. */
return; return;
loser: loser:
status = 1; status = 1;
bfd_nonfatal_message (NULL, obfd, osection, err); bfd_nonfatal_message (NULL, obfd, osection, err);
} }
@ -3525,9 +3527,9 @@ write_debugging_info (bfd *obfd, void *dhandle,
} }
/* We can get away with setting the section contents now because /* We can get away with setting the section contents now because
the next thing the caller is going to do is copy over the the next thing the caller is going to do is copy over the
real sections. We may someday have to split the contents real sections. We may someday have to split the contents
setting out of this function. */ setting out of this function. */
if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize) if (! bfd_set_section_contents (obfd, stabsec, syms, 0, symsize)
|| ! bfd_set_section_contents (obfd, stabstrsec, strings, 0, || ! bfd_set_section_contents (obfd, stabstrsec, strings, 0,
stringsize)) stringsize))
@ -3542,7 +3544,7 @@ write_debugging_info (bfd *obfd, void *dhandle,
bfd_nonfatal_message (NULL, obfd, NULL, bfd_nonfatal_message (NULL, obfd, NULL,
_("don't know how to write debugging information for %s"), _("don't know how to write debugging information for %s"),
bfd_get_target (obfd)); bfd_get_target (obfd));
return FALSE; return FALSE;
} }
@ -3850,8 +3852,8 @@ convert_efi_target (char *efi)
static struct section_add * static struct section_add *
init_section_add (const char *arg, init_section_add (const char *arg,
struct section_add *next, struct section_add *next,
const char *option) const char *option)
{ {
struct section_add *pa; struct section_add *pa;
const char *s; const char *s;
@ -3888,7 +3890,7 @@ section_add_load_file (struct section_add *pa)
f = fopen (pa->filename, FOPEN_RB); f = fopen (pa->filename, FOPEN_RB);
if (f == NULL) if (f == NULL)
fatal (_("cannot open: %s: %s"), fatal (_("cannot open: %s: %s"),
pa->filename, strerror (errno)); pa->filename, strerror (errno));
off = 0; off = 0;
alloc = 4096; alloc = 4096;
@ -3898,14 +3900,14 @@ section_add_load_file (struct section_add *pa)
off_t got; off_t got;
if (off == alloc) if (off == alloc)
{ {
alloc <<= 1; alloc <<= 1;
pa->contents = (bfd_byte *) xrealloc (pa->contents, alloc); pa->contents = (bfd_byte *) xrealloc (pa->contents, alloc);
} }
got = fread (pa->contents + off, 1, alloc - off, f); got = fread (pa->contents + off, 1, alloc - off, f);
if (ferror (f)) if (ferror (f))
fatal (_("%s: fread failed"), pa->filename); fatal (_("%s: fread failed"), pa->filename);
off += got; off += got;
} }
@ -4086,20 +4088,20 @@ copy_main (int argc, char *argv[])
break; break;
case OPTION_ADD_SECTION: case OPTION_ADD_SECTION:
add_sections = init_section_add (optarg, add_sections, add_sections = init_section_add (optarg, add_sections,
"--add-section"); "--add-section");
section_add_load_file (add_sections); section_add_load_file (add_sections);
break; break;
case OPTION_UPDATE_SECTION: case OPTION_UPDATE_SECTION:
update_sections = init_section_add (optarg, update_sections, update_sections = init_section_add (optarg, update_sections,
"--update-section"); "--update-section");
section_add_load_file (update_sections); section_add_load_file (update_sections);
break; break;
case OPTION_DUMP_SECTION: case OPTION_DUMP_SECTION:
dump_sections = init_section_add (optarg, dump_sections, dump_sections = init_section_add (optarg, dump_sections,
"--dump-section"); "--dump-section");
break; break;
case OPTION_ADD_SYMBOL: case OPTION_ADD_SYMBOL:
@ -4127,6 +4129,7 @@ copy_main (int argc, char *argv[])
} }
t = strchr (t + 1, ','); t = strchr (t + 1, ',');
newsym->othersym = NULL;
if (t) if (t)
newsym->flags = parse_symflags (t+1, &newsym->othersym); newsym->flags = parse_symflags (t+1, &newsym->othersym);
else else
@ -4522,39 +4525,39 @@ copy_main (int argc, char *argv[])
break; break;
case OPTION_REVERSE_BYTES: case OPTION_REVERSE_BYTES:
{ {
int prev = reverse_bytes; int prev = reverse_bytes;
reverse_bytes = atoi (optarg); reverse_bytes = atoi (optarg);
if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0)) if ((reverse_bytes <= 0) || ((reverse_bytes % 2) != 0))
fatal (_("number of bytes to reverse must be positive and even")); fatal (_("number of bytes to reverse must be positive and even"));
if (prev && prev != reverse_bytes) if (prev && prev != reverse_bytes)
non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"), non_fatal (_("Warning: ignoring previous --reverse-bytes value of %d"),
prev); prev);
break; break;
} }
case OPTION_FILE_ALIGNMENT: case OPTION_FILE_ALIGNMENT:
pe_file_alignment = parse_vma (optarg, "--file-alignment"); pe_file_alignment = parse_vma (optarg, "--file-alignment");
break; break;
case OPTION_HEAP: case OPTION_HEAP:
{ {
char *end; char *end;
pe_heap_reserve = strtoul (optarg, &end, 0); pe_heap_reserve = strtoul (optarg, &end, 0);
if (end == optarg if (end == optarg
|| (*end != '.' && *end != '\0')) || (*end != '.' && *end != '\0'))
non_fatal (_("%s: invalid reserve value for --heap"), non_fatal (_("%s: invalid reserve value for --heap"),
optarg); optarg);
else if (*end != '\0') else if (*end != '\0')
{ {
pe_heap_commit = strtoul (end + 1, &end, 0); pe_heap_commit = strtoul (end + 1, &end, 0);
if (*end != '\0') if (*end != '\0')
non_fatal (_("%s: invalid commit value for --heap"), non_fatal (_("%s: invalid commit value for --heap"),
optarg); optarg);
} }
} }
break; break;
case OPTION_IMAGE_BASE: case OPTION_IMAGE_BASE:
@ -4571,21 +4574,21 @@ copy_main (int argc, char *argv[])
break; break;
case OPTION_STACK: case OPTION_STACK:
{ {
char *end; char *end;
pe_stack_reserve = strtoul (optarg, &end, 0); pe_stack_reserve = strtoul (optarg, &end, 0);
if (end == optarg if (end == optarg
|| (*end != '.' && *end != '\0')) || (*end != '.' && *end != '\0'))
non_fatal (_("%s: invalid reserve value for --stack"), non_fatal (_("%s: invalid reserve value for --stack"),
optarg); optarg);
else if (*end != '\0') else if (*end != '\0')
{ {
pe_stack_commit = strtoul (end + 1, &end, 0); pe_stack_commit = strtoul (end + 1, &end, 0);
if (*end != '\0') if (*end != '\0')
non_fatal (_("%s: invalid commit value for --stack"), non_fatal (_("%s: invalid commit value for --stack"),
optarg); optarg);
} }
} }
break; break;
case 0: case 0: