/* Support for the generic parts of PE/PEI; the common executable parts.
- Copyright (C) 1995-2018 Free Software Foundation, Inc.
+ Copyright (C) 1995-2019 Free Software Foundation, Inc.
Written by Cygnus Solutions.
This file is part of BFD, the Binary File Descriptor library.
a->NumberOfRvaAndSizes = H_GET_32 (abfd, src->NumberOfRvaAndSizes);
{
- int idx;
+ unsigned idx;
/* PR 17512: Corrupt PE binaries can cause seg-faults. */
if (a->NumberOfRvaAndSizes > IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
{
/* xgettext:c-format */
_bfd_error_handler
- (_("%pB: aout header specifies an invalid number of data-directory entries: %ld"),
- abfd, a->NumberOfRvaAndSizes);
+ (_("%pB: aout header specifies an invalid number of"
+ " data-directory entries: %u"), abfd, a->NumberOfRvaAndSizes);
bfd_set_error (bfd_error_bad_value);
/* Paranoia: If the number is corrupt, then assume that the
H_PUT_16 (abfd, filehdr_in->f_magic, filehdr_out->f_magic);
H_PUT_16 (abfd, filehdr_in->f_nscns, filehdr_out->f_nscns);
- /* Only use a real timestamp if the option was chosen. */
+ /* Use a real timestamp by default, unless the no-insert-timestamp
+ option was chosen. */
if ((pe_data (abfd)->insert_timestamp))
H_PUT_32 (abfd, time (0), filehdr_out->f_timdat);
else
typedef struct
{
- const char * section_name;
+ char section_name[SCNNMLEN];
unsigned long must_have;
}
pe_required_section_flags;
{ ".text" , IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE },
{ ".tls", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE },
{ ".xdata", IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA },
- { NULL, 0}
};
pe_required_section_flags * p;
by ld --enable-auto-import (if auto-import is actually needed),
by ld --omagic, or by obcopy --writable-text. */
- for (p = known_sections; p->section_name; p++)
- if (strncmp (scnhdr_int->s_name, p->section_name,
- sizeof scnhdr_int->s_name) == 0)
+ for (p = known_sections;
+ p < known_sections + ARRAY_SIZE (known_sections);
+ p++)
+ if (memcmp (scnhdr_int->s_name, p->section_name, SCNNMLEN) == 0)
{
- if (strncmp (scnhdr_int->s_name, ".text", sizeof scnhdr_int->s_name)
+ if (memcmp (scnhdr_int->s_name, ".text", sizeof ".text")
|| (bfd_get_file_flags (abfd) & WP_TEXT))
scnhdr_int->s_flags &= ~IMAGE_SCN_MEM_WRITE;
scnhdr_int->s_flags |= p->must_have;
if (coff_data (abfd)->link_info
&& ! bfd_link_relocatable (coff_data (abfd)->link_info)
&& ! bfd_link_pic (coff_data (abfd)->link_info)
- && strncmp (scnhdr_int->s_name, ".text", sizeof scnhdr_int->s_name) == 0)
+ && memcmp (scnhdr_int->s_name, ".text", sizeof ".text") == 0)
{
/* By inference from looking at MS output, the 32 bit field
which is the combination of the number_of_relocs and
if (hint_addr == 0)
hint_addr = first_thunk;
- if (hint_addr != 0)
+ if (hint_addr != 0 && hint_addr - adj < datasize)
{
bfd_byte *ft_data;
asection *ft_section;
dataoff = addr - section->vma;
datasize = extra->DataDirectory[PE_EXPORT_TABLE].Size;
- if (datasize > section->size - dataoff)
+ if (dataoff > section->size
+ || datasize > section->size - dataoff)
{
fprintf (file,
_("\nThere is an export table in %s, but it does not fit into that section\n"),
}
/* PR 17512: Handle corrupt PE binaries. */
- if (datasize < 36)
+ if (datasize < 40)
{
fprintf (file,
/* xgettext:c-format */
edt.base);
/* PR 17512: Handle corrupt PE binaries. */
- if (edt.eat_addr + (edt.num_functions * 4) - adj >= datasize
+ /* PR 17512 file: 140-165018-0.004. */
+ if (edt.eat_addr - adj >= datasize
/* PR 17512: file: 092b1829 */
- || (edt.num_functions * 4) < edt.num_functions
- /* PR 17512 file: 140-165018-0.004. */
- || data + edt.eat_addr - adj < data)
+ || (edt.num_functions + 1) * 4 < edt.num_functions
+ || edt.eat_addr - adj + (edt.num_functions + 1) * 4 > datasize)
fprintf (file, _("\tInvalid Export Address Table rva (0x%lx) or entry count (0x%lx)\n"),
(long) edt.eat_addr,
(long) edt.num_functions);
fprintf (file, "\t(%s)",name);
fprintf (file, "\nMajorLinkerVersion\t%d\n", i->MajorLinkerVersion);
fprintf (file, "MinorLinkerVersion\t%d\n", i->MinorLinkerVersion);
- fprintf (file, "SizeOfCode\t\t%08lx\n", (unsigned long) i->SizeOfCode);
- fprintf (file, "SizeOfInitializedData\t%08lx\n",
- (unsigned long) i->SizeOfInitializedData);
- fprintf (file, "SizeOfUninitializedData\t%08lx\n",
- (unsigned long) i->SizeOfUninitializedData);
- fprintf (file, "AddressOfEntryPoint\t");
+ fprintf (file, "SizeOfCode\t\t");
+ bfd_fprintf_vma (abfd, file, i->SizeOfCode);
+ fprintf (file, "\nSizeOfInitializedData\t");
+ bfd_fprintf_vma (abfd, file, i->SizeOfInitializedData);
+ fprintf (file, "\nSizeOfUninitializedData\t");
+ bfd_fprintf_vma (abfd, file, i->SizeOfUninitializedData);
+ fprintf (file, "\nAddressOfEntryPoint\t");
bfd_fprintf_vma (abfd, file, i->AddressOfEntryPoint);
fprintf (file, "\nBaseOfCode\t\t");
bfd_fprintf_vma (abfd, file, i->BaseOfCode);
fprintf (file, "\nImageBase\t\t");
bfd_fprintf_vma (abfd, file, i->ImageBase);
- fprintf (file, "\nSectionAlignment\t");
- bfd_fprintf_vma (abfd, file, i->SectionAlignment);
- fprintf (file, "\nFileAlignment\t\t");
- bfd_fprintf_vma (abfd, file, i->FileAlignment);
- fprintf (file, "\nMajorOSystemVersion\t%d\n", i->MajorOperatingSystemVersion);
+ fprintf (file, "\nSectionAlignment\t%08x\n", i->SectionAlignment);
+ fprintf (file, "FileAlignment\t\t%08x\n", i->FileAlignment);
+ fprintf (file, "MajorOSystemVersion\t%d\n", i->MajorOperatingSystemVersion);
fprintf (file, "MinorOSystemVersion\t%d\n", i->MinorOperatingSystemVersion);
fprintf (file, "MajorImageVersion\t%d\n", i->MajorImageVersion);
fprintf (file, "MinorImageVersion\t%d\n", i->MinorImageVersion);
fprintf (file, "MajorSubsystemVersion\t%d\n", i->MajorSubsystemVersion);
fprintf (file, "MinorSubsystemVersion\t%d\n", i->MinorSubsystemVersion);
- fprintf (file, "Win32Version\t\t%08lx\n", (unsigned long) i->Reserved1);
- fprintf (file, "SizeOfImage\t\t%08lx\n", (unsigned long) i->SizeOfImage);
- fprintf (file, "SizeOfHeaders\t\t%08lx\n", (unsigned long) i->SizeOfHeaders);
- fprintf (file, "CheckSum\t\t%08lx\n", (unsigned long) i->CheckSum);
+ fprintf (file, "Win32Version\t\t%08x\n", i->Reserved1);
+ fprintf (file, "SizeOfImage\t\t%08x\n", i->SizeOfImage);
+ fprintf (file, "SizeOfHeaders\t\t%08x\n", i->SizeOfHeaders);
+ fprintf (file, "CheckSum\t\t%08x\n", i->CheckSum);
switch (i->Subsystem)
{
(struct external_IMAGE_DEBUG_DIRECTORY *)(data + (addr - section->vma));
/* PR 17512: file: 0f15796a. */
- if (ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size + (addr - section->vma)
- > bfd_get_section_size (section))
+ if ((unsigned long) ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size
+ > section->size - (addr - section->vma))
{
/* xgettext:c-format */
_bfd_error_handler
"exceeds space left in section (%" PRIx64 ")"),
obfd, ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size,
(uint64_t) (section->size - (addr - section->vma)));
- return FALSE;
- }
- /* PR 23110. */
- else if (ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size < 0)
- {
- /* xgettext:c-format */
- _bfd_error_handler
- (_("%pB: Data Directory size (%#lx) is negative"),
- obfd, ope->pe_opthdr.DataDirectory[PE_DEBUG_DATA].Size);
+ free (data);
return FALSE;
}
if (!bfd_set_section_contents (obfd, section, data, 0, section->size))
{
_bfd_error_handler (_("failed to update file offsets in debug directory"));
+ free (data);
return FALSE;
}
+ free (data);
}
else if (section)
{