Fix an illegal memory access when creating DLLs.

PR 29006
	* pe-dll.c (dll_name): Delete, replacing with..
	(dll_filename): ..this, moved earlier in file.
	(generate_edata): Delete parameters.  Don't set up dll_name here..
	(pe_process_import_defs): ..instead set up dll_filename and
	dll_symname here before returning.
	(dll_symname_len): Delete write-only variable.
	(pe_dll_generate_implib): Don't set up dll_symname here.
This commit is contained in:
Alan Modra
2022-05-12 12:55:20 +01:00
committed by Nick Clifton
parent 73a5cb257a
commit 678dc756a5
2 changed files with 131 additions and 124 deletions

View File

@ -1,3 +1,14 @@
2022-05-12 Alan Modra <amodra@gmail.com>
PR 29006
* pe-dll.c (dll_name): Delete, replacing with..
(dll_filename): ..this, moved earlier in file.
(generate_edata): Delete parameters. Don't set up dll_name here..
(pe_process_import_defs): ..instead set up dll_filename and
dll_symname here before returning.
(dll_symname_len): Delete write-only variable.
(pe_dll_generate_implib): Don't set up dll_symname here.
2022-05-03 Nick Clifton <nickc@redhat.com>
PR 29101

View File

@ -505,7 +505,7 @@ static int export_table_size;
static int count_exported;
static int count_exported_byname;
static int count_with_ordinals;
static const char *dll_name;
static const char *dll_filename;
static int min_ordinal, max_ordinal;
static int *exported_symbols;
@ -1066,25 +1066,13 @@ build_filler_bfd (int include_edata)
/* Gather all the exported symbols and build the .edata section. */
static void
generate_edata (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
generate_edata (void)
{
int i, next_ordinal;
int name_table_size = 0;
const char *dlnp;
/* First, we need to know how many exported symbols there are,
and what the range of ordinals is. */
if (pe_def_file->name)
dll_name = pe_def_file->name;
else
{
dll_name = bfd_get_filename (abfd);
for (dlnp = dll_name; *dlnp; dlnp++)
if (*dlnp == '\\' || *dlnp == '/' || *dlnp == ':')
dll_name = dlnp + 1;
}
if (count_with_ordinals && max_ordinal > count_exported)
{
if (min_ordinal > max_ordinal - count_exported + 1)
@ -1159,7 +1147,7 @@ generate_edata (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
+ 4 * export_table_size /* addresses */
+ 4 * count_exported_byname /* name ptrs */
+ 2 * count_exported_byname /* ordinals */
+ name_table_size + strlen (dll_name) + 1);
+ name_table_size + strlen (dll_filename) + 1);
}
/* Fill the exported symbol offsets. The preliminary work has already
@ -1232,7 +1220,7 @@ fill_edata (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
}
bfd_put_32 (abfd, ERVA (enamestr), edata_d + 12);
strcpy (enamestr, dll_name);
strcpy (enamestr, dll_filename);
enamestr += strlen (enamestr) + 1;
bfd_put_32 (abfd, min_ordinal, edata_d + 16);
bfd_put_32 (abfd, export_table_size, edata_d + 20);
@ -1971,9 +1959,7 @@ pe_dll_generate_def_file (const char *pe_out_def_filename)
static asymbol **symtab;
static int symptr;
static int tmp_seq;
static const char *dll_filename;
static char *dll_symname;
static int dll_symname_len;
#define UNDSEC bfd_und_section_ptr
@ -2835,7 +2821,8 @@ pe_create_import_fixup (arelent *rel, asection *s, bfd_vma addend, char *name,
printf ("creating runtime pseudo-reloc entry for %s (addend=%d)\n",
fixup_name, (int) addend);
b = make_runtime_pseudo_reloc (name, fixup_name, addend, rel->howto->bitsize,
b = make_runtime_pseudo_reloc (name, fixup_name, addend,
rel->howto->bitsize,
link_info.output_bfd);
add_bfd_to_link (b, bfd_get_filename (b), &link_info);
@ -2861,13 +2848,6 @@ pe_dll_generate_implib (def_file *def, const char *impfilename, struct bfd_link_
bfd *ibfd;
bfd *head = 0;
dll_filename = (def->name) ? def->name : dll_name;
dll_symname = xstrdup (dll_filename);
dll_symname_len = strlen (dll_symname);
for (i = 0; dll_symname[i]; i++)
if (!ISALNUM (dll_symname[i]))
dll_symname[i] = '_';
unlink_if_ordinary (impfilename);
outarch = bfd_openw (impfilename, 0);
@ -2995,8 +2975,7 @@ pe_dll_generate_implib (def_file *def, const char *impfilename, struct bfd_link_
}
}
n = make_one (def->exports + i, outarch,
! (def->exports + i)->flag_data);
n = make_one (def->exports + i, outarch, !(def->exports + i)->flag_data);
n->archive_next = head;
head = n;
def->exports[i].internal_name = internal;
@ -3207,16 +3186,15 @@ add_bfd_to_link (bfd *abfd, const char *name, struct bfd_link_info *linfo)
void
pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *linfo)
{
pe_dll_id_target (bfd_get_target (output_bfd));
if (pe_def_file)
{
int i, j;
def_file_module *module;
def_file_import *imp;
pe_dll_id_target (bfd_get_target (output_bfd));
if (!pe_def_file)
return;
imp = pe_def_file->imports;
pe_create_undef_table ();
@ -3225,14 +3203,14 @@ pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *linfo)
{
int do_this_dll = 0;
for (i = 0; i < pe_def_file->num_imports && imp[i].module != module; i++)
;
for (i = 0; i < pe_def_file->num_imports; i++)
if (imp[i].module == module)
break;
if (i >= pe_def_file->num_imports)
continue;
dll_filename = module->name;
dll_symname = xstrdup (module->name);
dll_symname_len = strlen (dll_symname);
for (j = 0; dll_symname[j]; j++)
if (!ISALNUM (dll_symname[j]))
dll_symname[j] = '_';
@ -3281,7 +3259,8 @@ pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *linfo)
is_undef = (blhe->type == bfd_link_hash_undefined);
}
if (is_cdecl && (!blhe || (blhe && blhe->type != bfd_link_hash_undefined)))
if (is_cdecl
&& (!blhe || (blhe && blhe->type != bfd_link_hash_undefined)))
{
sprintf (name, "%s%s",U (""), imp[i].internal_name);
blhe = pe_find_cdecl_alias_match (linfo, name);
@ -3299,7 +3278,8 @@ pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *linfo)
if (!do_this_dll)
{
bfd *ar_head = make_head (output_bfd);
add_bfd_to_link (ar_head, bfd_get_filename (ar_head), linfo);
add_bfd_to_link (ar_head, bfd_get_filename (ar_head),
linfo);
do_this_dll = 1;
}
exp.internal_name = imp[i].internal_name;
@ -3311,7 +3291,8 @@ pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *linfo)
exp.flag_constant = 0;
exp.flag_data = imp[i].data;
exp.flag_noname = exp.name ? 0 : 1;
one = make_one (&exp, output_bfd, (! exp.flag_data) && include_jmp_stub);
one = make_one (&exp, output_bfd,
!exp.flag_data && include_jmp_stub);
add_bfd_to_link (one, bfd_get_filename (one), linfo);
}
}
@ -3332,6 +3313,21 @@ pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *linfo)
free (udef_table);
}
if (pe_def_file && pe_def_file->name)
dll_filename = pe_def_file->name;
else
{
dll_filename = bfd_get_filename (output_bfd);
for (const char *p = dll_filename; *p; p++)
if (*p == '\\' || *p == '/' || *p == ':')
dll_filename = p + 1;
}
dll_symname = xstrdup (dll_filename);
for (int i = 0; dll_symname[i]; i++)
if (!ISALNUM (dll_symname[i]))
dll_symname[i] = '_';
}
/* We were handed a *.DLL file. Parse it and turn it into a set of
IMPORTS directives in the def file. Return TRUE if the file was
handled, FALSE if not. */
@ -3629,7 +3625,7 @@ pe_dll_build_sections (bfd *abfd, struct bfd_link_info *info)
return;
}
generate_edata (abfd, info);
generate_edata ();
build_filler_bfd (1);
pe_output_file_set_long_section_names (filler_bfd);
}