Skip to content

Commit d1e9f4d

Browse files
MBODAPATalpsayin
authored andcommitted
microblaze: Double free with ld --no-keep-memory. Proposed patches from the community member (dednev@rambler.ru) for 2021.1. [CR-1115233]
Conflicts: bfd/elf32-microblaze.c bfd/elf64-microblaze.c Signed-off-by: Aayush Misra <aayushm@amd.com> Patchfile taken from: https: //github.com/Xilinx/meta-xilinx/blob/master/meta-microblaze/recipes-devtools/binutils/binutils/0037-Double-free-with-ld-no-keep-memory.-Proposed-patches.patch Signed-off-by: Alp Sayin <alpsayin@gmail.com>
1 parent 5cbe414 commit d1e9f4d

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

bfd/elf32-microblaze.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,10 +1766,8 @@ microblaze_elf_relax_section (bfd *abfd,
17661766
{
17671767
Elf_Internal_Shdr *symtab_hdr;
17681768
Elf_Internal_Rela *internal_relocs;
1769-
Elf_Internal_Rela *free_relocs = NULL;
17701769
Elf_Internal_Rela *irel, *irelend;
17711770
bfd_byte *contents = NULL;
1772-
bfd_byte *free_contents = NULL;
17731771
int rel_count;
17741772
unsigned int shndx;
17751773
size_t i, sym_index;
@@ -1813,8 +1811,6 @@ microblaze_elf_relax_section (bfd *abfd,
18131811
internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, link_info->keep_memory);
18141812
if (internal_relocs == NULL)
18151813
goto error_return;
1816-
if (! link_info->keep_memory)
1817-
free_relocs = internal_relocs;
18181814

18191815
sdata->relax_count = 0;
18201816
sdata->relax = (struct relax_table *) bfd_malloc ((sec->reloc_count + 1)
@@ -1842,7 +1838,6 @@ microblaze_elf_relax_section (bfd *abfd,
18421838
contents = (bfd_byte *) bfd_malloc (sec->size);
18431839
if (contents == NULL)
18441840
goto error_return;
1845-
free_contents = contents;
18461841

18471842
if (!bfd_get_section_contents (abfd, sec, contents,
18481843
(file_ptr) 0, sec->size))
@@ -2288,25 +2283,26 @@ microblaze_elf_relax_section (bfd *abfd,
22882283
}
22892284

22902285
elf_section_data (sec)->relocs = internal_relocs;
2291-
free_relocs = NULL;
22922286

22932287
elf_section_data (sec)->this_hdr.contents = contents;
2294-
free_contents = NULL;
22952288

22962289
symtab_hdr->contents = (bfd_byte *) isymbuf;
22972290
}
22982291

2299-
free (free_relocs);
2300-
free_relocs = NULL;
2292+
if (internal_relocs != NULL
2293+
&& elf_section_data (sec)->relocs != internal_relocs)
2294+
free (internal_relocs);
23012295

2302-
if (free_contents != NULL)
2303-
{
2304-
if (!link_info->keep_memory)
2305-
free (free_contents);
2296+
if (contents != NULL
2297+
&& elf_section_data (sec)->this_hdr.contents != contents)
2298+
{
2299+
if (! link_info->keep_memory)
2300+
free (contents);
23062301
else
2307-
/* Cache the section contents for elf_link_input_bfd. */
2308-
elf_section_data (sec)->this_hdr.contents = contents;
2309-
free_contents = NULL;
2302+
{
2303+
/* Cache the section contents for elf_link_input_bfd. */
2304+
elf_section_data (sec)->this_hdr.contents = contents;
2305+
}
23102306
}
23112307

23122308
if (sdata->relax_count == 0)
@@ -2320,8 +2316,16 @@ microblaze_elf_relax_section (bfd *abfd,
23202316
return true;
23212317

23222318
error_return:
2323-
free (free_relocs);
2324-
free (free_contents);
2319+
2320+
if (isymbuf != NULL
2321+
&& symtab_hdr->contents != (unsigned char *) isymbuf)
2322+
free (isymbuf);
2323+
if (internal_relocs != NULL
2324+
&& elf_section_data (sec)->relocs != internal_relocs)
2325+
free (internal_relocs);
2326+
if (contents != NULL
2327+
&& elf_section_data (sec)->this_hdr.contents != contents)
2328+
free (contents);
23252329
free (sdata->relax);
23262330
sdata->relax = NULL;
23272331
sdata->relax_count = 0;

0 commit comments

Comments
 (0)