BaseTools/GenFw: Enhance error message for bad symbol definitions

This patch expands the error message that is output when GenFw
encounters a bad symbol definition or an unsupported symbol type.  It
displays the symbol name, the symbol address, and a message that
describes both possibilities (bad symbol definition or unsupported
symbol type).  It also provides two examples of unsupported symbol
types.

Furthermore, this patch revises the conditional for detecting bad
symbol definitions to eliminate a redundant test (a Sym->st_shndx
value of SHN_ABS should certainly be greater than mEhdr->e_shnum) and
to change another test from 'Sym->st_shndx > mEhdr->e_shnum' to
'Sym->st_shndx >= mEhdr->e_shnum' for consistency with the test in
GetShdrByIndex.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Michael LeMay <michael.lemay@intel.com>
Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com>
This commit is contained in:
Michael LeMay 2016-01-29 12:17:13 +08:00 committed by Yonghong Zhu
parent 17751c5fa4
commit 621bb723a4
3 changed files with 119 additions and 6 deletions

View File

@ -266,6 +266,53 @@ IsDataShdr (
return (BOOLEAN) (Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == (SHF_ALLOC | SHF_WRITE);
}
STATIC
BOOLEAN
IsStrtabShdr (
Elf_Shdr *Shdr
)
{
Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx);
return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_STRTAB_SECTION_NAME) == 0);
}
STATIC
Elf_Shdr *
FindStrtabShdr (
VOID
)
{
UINT32 i;
for (i = 0; i < mEhdr->e_shnum; i++) {
Elf_Shdr *shdr = GetShdrByIndex(i);
if (IsStrtabShdr(shdr)) {
return shdr;
}
}
return NULL;
}
STATIC
const UINT8 *
GetSymName (
Elf_Sym *Sym
)
{
if (Sym->st_name == 0) {
return NULL;
}
Elf_Shdr *StrtabShdr = FindStrtabShdr();
if (StrtabShdr == NULL) {
return NULL;
}
assert(Sym->st_name < StrtabShdr->sh_size);
return (UINT8*)mEhdr + StrtabShdr->sh_offset + Sym->st_name;
}
//
// Elf functions interface implementation
//
@ -671,9 +718,18 @@ WriteSections32 (
// header location.
//
if (Sym->st_shndx == SHN_UNDEF
|| Sym->st_shndx == SHN_ABS
|| Sym->st_shndx > mEhdr->e_shnum) {
Error (NULL, 0, 3000, "Invalid", "%s bad symbol definition.", mInImageName);
|| Sym->st_shndx >= mEhdr->e_shnum) {
const UINT8 *SymName = GetSymName(Sym);
if (SymName == NULL) {
SymName = (const UINT8 *)"<unknown>";
}
Error (NULL, 0, 3000, "Invalid",
"%s: Bad definition for symbol '%s'@%p or unsupported symbol type. "
"For example, absolute and undefined symbols are not supported.",
mInImageName, SymName, Sym->st_value);
exit(EXIT_FAILURE);
}
SymShdr = GetShdrByIndex(Sym->st_shndx);

View File

@ -258,6 +258,53 @@ IsDataShdr (
return (BOOLEAN) (Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == (SHF_ALLOC | SHF_WRITE);
}
STATIC
BOOLEAN
IsStrtabShdr (
Elf_Shdr *Shdr
)
{
Elf_Shdr *Namedr = GetShdrByIndex(mEhdr->e_shstrndx);
return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_STRTAB_SECTION_NAME) == 0);
}
STATIC
Elf_Shdr *
FindStrtabShdr (
VOID
)
{
UINT32 i;
for (i = 0; i < mEhdr->e_shnum; i++) {
Elf_Shdr *shdr = GetShdrByIndex(i);
if (IsStrtabShdr(shdr)) {
return shdr;
}
}
return NULL;
}
STATIC
const UINT8 *
GetSymName (
Elf_Sym *Sym
)
{
if (Sym->st_name == 0) {
return NULL;
}
Elf_Shdr *StrtabShdr = FindStrtabShdr();
if (StrtabShdr == NULL) {
return NULL;
}
assert(Sym->st_name < StrtabShdr->sh_size);
return (UINT8*)mEhdr + StrtabShdr->sh_offset + Sym->st_name;
}
//
// Elf functions interface implementation
//
@ -667,9 +714,18 @@ WriteSections64 (
// header location.
//
if (Sym->st_shndx == SHN_UNDEF
|| Sym->st_shndx == SHN_ABS
|| Sym->st_shndx > mEhdr->e_shnum) {
Error (NULL, 0, 3000, "Invalid", "%s bad symbol definition.", mInImageName);
|| Sym->st_shndx >= mEhdr->e_shnum) {
const UINT8 *SymName = GetSymName(Sym);
if (SymName == NULL) {
SymName = (const UINT8 *)"<unknown>";
}
Error (NULL, 0, 3000, "Invalid",
"%s: Bad definition for symbol '%s'@%p or unsupported symbol type. "
"For example, absolute and undefined symbols are not supported.",
mInImageName, SymName, Sym->st_value);
exit(EXIT_FAILURE);
}
SymShdr = GetShdrByIndex(Sym->st_shndx);

View File

@ -34,6 +34,7 @@ extern UINT32 mOutImageType;
// Common EFI specific data.
//
#define ELF_HII_SECTION_NAME ".hii"
#define ELF_STRTAB_SECTION_NAME ".strtab"
#define MAX_COFF_ALIGNMENT 0x10000
//