/ bfd_octets_per_byte (abfd, sec));
}
+static inline bfd_size_type
+bfd_get_section_alloc_size (const bfd *abfd, const asection *sec)
+{
+ if (abfd->direction != write_direction && sec->rawsize > sec->size)
+ return sec->rawsize;
+ return sec->size;
+}
+
/* Functions to handle insertion and deletion of a bfd's sections. These
only handle the list pointers, ie. do not adjust section_count,
target_index etc. */
. / bfd_octets_per_byte (abfd, sec));
.}
.
+.static inline bfd_size_type
+.bfd_get_section_alloc_size (const bfd *abfd, const asection *sec)
+.{
+. if (abfd->direction != write_direction && sec->rawsize > sec->size)
+. return sec->rawsize;
+. return sec->size;
+.}
+.
.{* Functions to handle insertion and deletion of a bfd's sections. These
. only handle the list pointers, ie. do not adjust section_count,
. target_index etc. *}
if (reloc_size < 0)
return NULL;
+ bfd_byte *orig_data = data;
if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
return NULL;
reloc_vector = (arelent **) bfd_malloc (reloc_size);
if (reloc_vector == NULL)
- return NULL;
+ goto error_return;
reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
reloc_vector, symbols);
error_return:
free (reloc_vector);
+ if (orig_data == NULL)
+ free (data);
return NULL;
}
relocatable,
symbols);
+ bfd_byte *orig_data = data;
+ if (data == NULL)
+ {
+ data = bfd_malloc (input_section->size);
+ if (data == NULL)
+ return NULL;
+ }
memcpy (data, coff_section_data (input_bfd, input_section)->contents,
(size_t) input_section->size);
free (internal_relocs);
free (internal_syms);
free (sections);
+ if (orig_data == NULL)
+ free (data);
return NULL;
}
bool
bfd_get_full_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **ptr)
{
- bfd_size_type sz = bfd_get_section_limit_octets (abfd, sec);
+ bfd_size_type readsz = bfd_get_section_limit_octets (abfd, sec);
+ bfd_size_type allocsz = bfd_get_section_alloc_size (abfd, sec);
bfd_byte *p = *ptr;
bool ret;
bfd_size_type save_size;
unsigned int compression_header_size;
const unsigned int compress_status = sec->compress_status;
- if (sz == 0)
+ if (allocsz == 0)
{
*ptr = NULL;
return true;
_bfd_error_handler
/* xgettext:c-format */
(_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
- abfd, sec, (uint64_t) sz);
+ abfd, sec, (uint64_t) readsz);
return false;
}
case COMPRESS_SECTION_NONE:
if (p == NULL)
{
- p = (bfd_byte *) bfd_malloc (sz);
+ p = (bfd_byte *) bfd_malloc (allocsz);
if (p == NULL)
{
/* PR 20801: Provide a more helpful error message. */
_bfd_error_handler
/* xgettext:c-format */
(_("error: %pB(%pA) is too large (%#" PRIx64 " bytes)"),
- abfd, sec, (uint64_t) sz);
+ abfd, sec, (uint64_t) allocsz);
return false;
}
}
- if (!bfd_get_section_contents (abfd, sec, p, 0, sz))
+ if (!bfd_get_section_contents (abfd, sec, p, 0, readsz))
{
if (*ptr != p)
free (p);
goto fail_compressed;
if (p == NULL)
- p = (bfd_byte *) bfd_malloc (sz);
+ p = (bfd_byte *) bfd_malloc (allocsz);
if (p == NULL)
goto fail_compressed;
bool is_zstd = compress_status == DECOMPRESS_SECTION_ZSTD;
if (!decompress_contents (
is_zstd, compressed_buffer + compression_header_size,
- sec->compressed_size - compression_header_size, p, sz))
+ sec->compressed_size - compression_header_size, p, readsz))
{
bfd_set_error (bfd_error_bad_value);
if (p != *ptr)
return false;
if (p == NULL)
{
- p = (bfd_byte *) bfd_malloc (sz);
+ p = (bfd_byte *) bfd_malloc (allocsz);
if (p == NULL)
return false;
*ptr = p;
}
/* PR 17512; file: 5bc29788. */
if (p != sec->contents)
- memcpy (p, sec->contents, sz);
+ memcpy (p, sec->contents, readsz);
return true;
default:
symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
+ bfd_byte *orig_data = data;
+ if (data == NULL)
+ {
+ data = bfd_malloc (input_section->size);
+ if (data == NULL)
+ return NULL;
+ }
memcpy (data, elf_section_data (input_section)->this_hdr.contents,
(size_t) input_section->size);
free (isymbuf);
if (elf_section_data (input_section)->relocs != internal_relocs)
free (internal_relocs);
+ if (orig_data == NULL)
+ free (data);
return NULL;
}
symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
+ bfd_byte *orig_data = data;
+ if (data == NULL)
+ {
+ data = bfd_malloc (input_section->size);
+ if (data == NULL)
+ return NULL;
+ }
memcpy (data, elf_section_data (input_section)->this_hdr.contents,
(size_t) input_section->size);
free (isymbuf);
if (internal_relocs != elf_section_data (input_section)->relocs)
free (internal_relocs);
+ if (orig_data == NULL)
+ free (data);
return NULL;
}
symbols);
symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
+ bfd_byte *orig_data = data;
+ if (data == NULL)
+ {
+ data = bfd_malloc (input_section->size);
+ if (data == NULL)
+ return NULL;
+ }
memcpy (data, elf_section_data (input_section)->this_hdr.contents,
(size_t) input_section->size);
free (isymbuf);
if (elf_section_data (input_section)->relocs != internal_relocs)
free (internal_relocs);
+ if (orig_data == NULL)
+ free (data);
return NULL;
}
symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
+ bfd_byte *orig_data = data;
+ if (data == NULL)
+ {
+ data = bfd_malloc (input_section->size);
+ if (data == NULL)
+ return NULL;
+ }
memcpy (data, elf_section_data (input_section)->this_hdr.contents,
(size_t) input_section->size);
free (isymbuf);
if (elf_section_data (input_section)->relocs != internal_relocs)
free (internal_relocs);
+ if (orig_data == NULL)
+ free (data);
return NULL;
}
symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
+ bfd_byte *orig_data = data;
+ if (data == NULL)
+ {
+ data = bfd_malloc (input_section->size);
+ if (data == NULL)
+ return NULL;
+ }
memcpy (data, elf_section_data (input_section)->this_hdr.contents,
(size_t) input_section->size);
free (isymbuf);
if (elf_section_data (input_section)->relocs != internal_relocs)
free (internal_relocs);
+ if (orig_data == NULL)
+ free (data);
return NULL;
}
symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
+ bfd_byte *orig_data = data;
+ if (data == NULL)
+ {
+ data = bfd_malloc (input_section->size);
+ if (data == NULL)
+ return NULL;
+ }
memcpy (data, elf_section_data (input_section)->this_hdr.contents,
(size_t) input_section->size);
free (isymbuf);
if (elf_section_data (input_section)->relocs != internal_relocs)
free (internal_relocs);
+ if (orig_data == NULL)
+ free (data);
return NULL;
}
return NULL;
/* Read in the section. */
+ bfd_byte *orig_data = data;
if (!nds32_get_section_contents (input_bfd, input_section, &data, false))
return NULL;
reloc_vector = (arelent **) bfd_malloc (reloc_size);
if (reloc_vector == NULL)
- return NULL;
+ goto error_return;
reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
reloc_vector, symbols);
error_return:
free (reloc_vector);
+ if (orig_data == NULL)
+ free (data);
return NULL;
}
symtab_hdr = &elf_symtab_hdr (input_bfd);
+ bfd_byte *orig_data = data;
+ if (data == NULL)
+ {
+ data = bfd_malloc (input_section->size);
+ if (data == NULL)
+ return NULL;
+ }
memcpy (data, elf_section_data (input_section)->this_hdr.contents,
(size_t) input_section->size);
free (isymbuf);
if (elf_section_data (input_section)->relocs != internal_relocs)
free (internal_relocs);
+ if (orig_data == NULL)
+ free (data);
return NULL;
}
return NULL;
/* Read in the section. */
+ bfd_byte *orig_data = data;
if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
return NULL;
else
hip = &hi->next;
}
+ if (orig_data == NULL)
+ free (data);
data = NULL;
goto out;
}
{
asection *input_section;
bfd *input_bfd;
- bfd_byte *contents = NULL;
+ bfd_byte *alloced = NULL;
bfd_byte *new_contents;
- bfd_size_type sec_size;
file_ptr loc;
BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
else
{
/* Get and relocate the section contents. */
- sec_size = (input_section->rawsize > input_section->size
- ? input_section->rawsize
- : input_section->size);
- contents = (bfd_byte *) bfd_malloc (sec_size);
- if (contents == NULL && sec_size != 0)
- goto error_return;
new_contents = (bfd_get_relocated_section_contents
- (output_bfd, info, link_order, contents,
+ (output_bfd, info, link_order, NULL,
bfd_link_relocatable (info),
_bfd_generic_link_get_symbols (input_bfd)));
+ alloced = new_contents;
if (!new_contents)
goto error_return;
}
new_contents, loc, input_section->size))
goto error_return;
- free (contents);
+ free (alloced);
return true;
error_return:
- free (contents);
+ free (alloced);
return false;
}
return NULL;
/* Read in the section. */
+ bfd_byte *orig_data = data;
if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
return NULL;
reloc_vector = (arelent **) bfd_malloc (reloc_size);
if (reloc_vector == NULL)
- return NULL;
+ goto error_return;
reloc_count = bfd_canonicalize_reloc (input_bfd,
input_section,
error_return:
free (reloc_vector);
+ if (orig_data == NULL)
+ free (data);
return NULL;
}
/* Get enough memory to hold the stuff. */
bfd *input_bfd = link_order->u.indirect.section->owner;
asection *input_section = link_order->u.indirect.section;
- long reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
+ long reloc_size;
arelent **reloc_vector;
long reloc_count;
- bfd_size_type sz;
+ reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
if (reloc_size < 0)
return NULL;
symbols);
/* Read in the section. */
- sz = input_section->rawsize ? input_section->rawsize : input_section->size;
- if (!bfd_get_section_contents (input_bfd, input_section, data, 0, sz))
+ bfd_byte *orig_data = data;
+ if (!bfd_get_full_section_contents (input_bfd, input_section, &data))
return NULL;
- reloc_vector = (arelent **) bfd_malloc ((bfd_size_type) reloc_size);
- if (!reloc_vector && reloc_size != 0)
+ if (data == NULL)
return NULL;
+ if (reloc_size == 0)
+ return data;
+
+ reloc_vector = (arelent **) bfd_malloc (reloc_size);
+ if (reloc_vector == NULL)
+ goto error_return;
+
reloc_count = bfd_canonicalize_reloc (input_bfd,
input_section,
reloc_vector,
symbols);
if (reloc_count < 0)
- {
- free (reloc_vector);
- return NULL;
- }
+ goto error_return;
if (reloc_count > 0)
{
}
}
}
- free ((char *) reloc_vector);
+ free (reloc_vector);
return data;
+
+ error_return:
+ free (reloc_vector);
+ if (orig_data == NULL)
+ free (data);
+ return NULL;
}
struct bfd_link_info link_info;
struct bfd_link_order link_order;
struct bfd_link_callbacks callbacks;
- bfd_byte *contents, *data;
+ bfd_byte *contents;
struct saved_offsets saved_offsets;
bfd *link_next;
link_order.size = sec->size;
link_order.u.indirect.section = sec;
- data = NULL;
contents = NULL;
- if (outbuf == NULL)
- {
- bfd_size_type amt = sec->rawsize > sec->size ? sec->rawsize : sec->size;
- data = (bfd_byte *) bfd_malloc (amt);
- if (data == NULL)
- goto out1;
- outbuf = data;
- }
saved_offsets.section_count = abfd->section_count;
saved_offsets.sections = malloc (sizeof (*saved_offsets.sections)
* saved_offsets.section_count);
if (saved_offsets.sections == NULL)
- goto out2;
+ goto out1;
bfd_map_over_sections (abfd, simple_save_output_info, &saved_offsets);
if (symbol_table == NULL)
{
if (!bfd_generic_link_read_symbols (abfd))
- goto out3;
+ goto out2;
symbol_table = _bfd_generic_link_get_symbols (abfd);
}
outbuf,
false,
symbol_table);
- out3:
+ out2:
bfd_map_over_sections (abfd, simple_restore_output_info, &saved_offsets);
free (saved_offsets.sections);
- out2:
- if (contents == NULL)
- free (data);
out1:
_bfd_generic_link_hash_table_free (abfd);
abfd->link.next = link_next;