summaryrefslogtreecommitdiff
path: root/bfd/elflink.c
diff options
context:
space:
mode:
authorH.J. Lu <hjl.tools@gmail.com>2018-02-27 12:22:58 -0800
committerH.J. Lu <hjl.tools@gmail.com>2021-07-08 09:59:28 -0700
commita8dde0a2114f87bcdc19946aeab26788f5eae1b7 (patch)
tree52c8cd4ecd4d2e1a296652841e4e9d63d61ed5a4 /bfd/elflink.c
parent74b10a3219e44ba2585e3f7226a6455d41e92c1b (diff)
ld: Limit cache size and add --max-cache-size=SIZE
When link_info.keep_memory is true, linker caches the relocation information and symbol tables of input files in memory. When there are many input files with many relocations, we may run out of memory. Add --max-cache-size=SIZE to set the maximum cache size. bfd/ PR ld/18028 * bfd.c (bfd): Add alloc_size. * elf-bfd.h (_bfd_elf_link_info_read_relocs): New. * elf32-i386.c (elf_i386_check_relocs): Use _bfd_link_keep_memory. Update cache_size. * elf64-x86-64.c (elf_x86_64_check_relocs): Likewise. * elflink.c (_bfd_elf_link_read_relocs): Renamed to ... (_bfd_elf_link_info_read_relocs): This. Update cache_size. (_bfd_elf_link_read_relocs): New. (_bfd_elf_link_check_relocs): Call _bfd_elf_link_info_read_relocs instead of _bfd_elf_link_read_relocs. (elf_link_add_object_symbols): Likewise. (elf_link_input_bfd): Likewise. (init_reloc_cookie_rels): Likewise. (init_reloc_cookie): Update cache_size. Call _bfd_elf_link_info_read_relocs instead of _bfd_elf_link_read_relocs. (link_info_ok): New. (elf_gc_smash_unused_vtentry_relocs): Updated. Call _bfd_elf_link_info_read_relocs instead of _bfd_elf_link_read_relocs. (bfd_elf_gc_sections): Use link_info_ok. Pass &link_info_ok to elf_gc_smash_unused_vtentry_relocs. * libbfd-in.h (_bfd_link_keep_memory): New. * linker.c (_bfd_link_keep_memory): New. * opncls.c (bfd_alloc): Update alloc_size. * bfd-in2.h: Regenerated. * libbfd.h: Likewise. include/ PR ld/18028 * bfdlink.h (bfd_link_info): Add cache_size and max_cache_size. ld/ PR ld/18028 * NEWS: Mention --max-cache-size=SIZE. * ld.texi: Document --max-cache-size=SIZE. * ldlex.h (option_values): Add OPTION_MAX_CACHE_SIZE. * ldmain.c: (main): Set link_info.max_cache_size to -1. * lexsup.c (ld_options): Add --max-cache-size=SIZE. (parse_args): Support OPTION_MAX_CACHE_SIZE. * testsuite/ld-bootstrap/bootstrap.exp: Add test for --max-cache-size=-1.
Diffstat (limited to 'bfd/elflink.c')
-rw-r--r--bfd/elflink.c90
1 files changed, 67 insertions, 23 deletions
diff --git a/bfd/elflink.c b/bfd/elflink.c
index 9a05208253..003c9546b0 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -2619,14 +2619,16 @@ elf_link_read_relocs_from_section (bfd *abfd,
according to the KEEP_MEMORY argument. If O has two relocation
sections (both REL and RELA relocations), then the REL_HDR
relocations will appear first in INTERNAL_RELOCS, followed by the
- RELA_HDR relocations. */
+ RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true,
+ update cache_size. */
Elf_Internal_Rela *
-_bfd_elf_link_read_relocs (bfd *abfd,
- asection *o,
- void *external_relocs,
- Elf_Internal_Rela *internal_relocs,
- bool keep_memory)
+_bfd_elf_link_info_read_relocs (bfd *abfd,
+ struct bfd_link_info *info,
+ asection *o,
+ void *external_relocs,
+ Elf_Internal_Rela *internal_relocs,
+ bool keep_memory)
{
void *alloc1 = NULL;
Elf_Internal_Rela *alloc2 = NULL;
@@ -2646,7 +2648,11 @@ _bfd_elf_link_read_relocs (bfd *abfd,
size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela);
if (keep_memory)
- internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
+ {
+ internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
+ if (info)
+ info->cache_size += size;
+ }
else
internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
if (internal_relocs == NULL)
@@ -2710,6 +2716,22 @@ _bfd_elf_link_read_relocs (bfd *abfd,
return NULL;
}
+/* This is similar to _bfd_elf_link_info_read_relocs, except for that
+ NULL is passed to _bfd_elf_link_info_read_relocs for pointer to
+ struct bfd_link_info. */
+
+Elf_Internal_Rela *
+_bfd_elf_link_read_relocs (bfd *abfd,
+ asection *o,
+ void *external_relocs,
+ Elf_Internal_Rela *internal_relocs,
+ bool keep_memory)
+{
+ return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs,
+ internal_relocs, keep_memory);
+
+}
+
/* Compute the size of, and allocate space for, REL_HDR which is the
section header for a section containing relocations for O. */
@@ -4026,8 +4048,10 @@ _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
|| bfd_is_abs_section (o->output_section))
continue;
- internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL,
- info->keep_memory);
+ internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
+ o, NULL,
+ NULL,
+ _bfd_link_keep_memory (info));
if (internal_relocs == NULL)
return false;
@@ -5356,9 +5380,10 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
&& (s->flags & SEC_DEBUGGING) != 0))
continue;
- internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL,
- NULL,
- info->keep_memory);
+ internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info,
+ s, NULL,
+ NULL,
+ _bfd_link_keep_memory (info));
if (internal_relocs == NULL)
goto error_free_vers;
@@ -11186,8 +11211,10 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
/* Get the swapped relocs. */
internal_relocs
- = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs,
- flinfo->internal_relocs, false);
+ = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o,
+ flinfo->external_relocs,
+ flinfo->internal_relocs,
+ false);
if (internal_relocs == NULL
&& o->reloc_count > 0)
return false;
@@ -13279,8 +13306,12 @@ init_reloc_cookie (struct elf_reloc_cookie *cookie,
info->callbacks->einfo (_("%P%X: can not read symbols: %E\n"));
return false;
}
- if (info->keep_memory)
- symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
+ if (_bfd_link_keep_memory (info) )
+ {
+ symtab_hdr->contents = (bfd_byte *) cookie->locsyms;
+ info->cache_size += (cookie->locsymcount
+ * sizeof (Elf_External_Sym_Shndx));
+ }
}
return true;
}
@@ -13312,8 +13343,9 @@ init_reloc_cookie_rels (struct elf_reloc_cookie *cookie,
}
else
{
- cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
- info->keep_memory);
+ cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec,
+ NULL, NULL,
+ _bfd_link_keep_memory (info));
if (cookie->rels == NULL)
return false;
cookie->rel = cookie->rels;
@@ -13881,14 +13913,22 @@ elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp)
return true;
}
+struct link_info_ok
+{
+ struct bfd_link_info *info;
+ bool ok;
+};
+
static bool
-elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
+elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h,
+ void *ptr)
{
asection *sec;
bfd_vma hstart, hend;
Elf_Internal_Rela *relstart, *relend, *rel;
const struct elf_backend_data *bed;
unsigned int log_file_align;
+ struct link_info_ok *info = (struct link_info_ok *) ptr;
/* Take care of both those symbols that do not describe vtables as
well as those that are not loaded. */
@@ -13904,9 +13944,10 @@ elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp)
hstart = h->root.u.def.value;
hend = hstart + h->size;
- relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, true);
+ relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info,
+ sec, NULL, NULL, true);
if (!relstart)
- return *(bool *) okp = false;
+ return info->ok = false;
bed = get_elf_backend_data (sec->owner);
log_file_align = bed->s->log_file_align;
@@ -14029,6 +14070,7 @@ bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
elf_gc_mark_hook_fn gc_mark_hook;
const struct elf_backend_data *bed = get_elf_backend_data (abfd);
struct elf_link_hash_table *htab;
+ struct link_info_ok info_ok;
if (!bed->can_gc_sections
|| !is_elf_hash_table (info->hash))
@@ -14070,8 +14112,10 @@ bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
return false;
/* Kill the vtable relocations that were not used. */
- elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok);
- if (!ok)
+ info_ok.info = info;
+ info_ok.ok = true;
+ elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok);
+ if (!info_ok.ok)
return false;
/* Mark dynamically referenced symbols. */