Skip to content

Commit 898853b

Browse files
committed
Add trace messages for elf parser failures
These messages are useful for toolchain debugging. Signed-off-by: Dionna Glaze <[email protected]>
1 parent 50d5bec commit 898853b

File tree

2 files changed

+64
-22
lines changed

2 files changed

+64
-22
lines changed

psw/urts/parser/elfparser.cpp

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,28 +111,49 @@ const T* get_section_raw_data(const ElfW(Ehdr) *elf_hdr, ElfW(Addr) start_addr)
111111
bool validate_elf_header(const ElfW(Ehdr) *elf_hdr)
112112
{
113113
// validate magic number
114-
if (memcmp(&elf_hdr->e_ident, ELFMAG, SELFMAG))
114+
if (memcmp(&elf_hdr->e_ident, ELFMAG, SELFMAG)) {
115+
SE_TRACE(SE_TRACE_ERROR, "Incorrect magic number\n");
115116
return false;
117+
}
116118

117119
#if RTS_SYSTEM_WORDSIZE == 64
118-
if (ELFCLASS64 != elf_hdr->e_ident[EI_CLASS])
120+
if (ELFCLASS64 != elf_hdr->e_ident[EI_CLASS]) {
121+
SE_TRACE(SE_TRACE_ERROR, "Expected ELFCLASS64: 0x%x\n",
122+
elf_hdr->e_ident[EI_CLASS]);
119123
return false;
124+
}
120125
#else
121-
if (ELFCLASS32 != elf_hdr->e_ident[EI_CLASS])
126+
if (ELFCLASS32 != elf_hdr->e_ident[EI_CLASS]) {
127+
SE_TRACE(SE_TRACE_ERROR, "Expected ELFCLASS32: 0x%x\n",
128+
elf_hdr->e_ident[EI_CLASS]);
122129
return false;
130+
}
123131
#endif
124132

125-
if (ELFDATA2LSB!= elf_hdr->e_ident[EI_DATA])
133+
if (ELFDATA2LSB!= elf_hdr->e_ident[EI_DATA]) {
134+
SE_TRACE(SE_TRACE_ERROR, "Expected ELFDATA2LSB: 0x%x\n",
135+
elf_hdr->e_ident[EI_DATA]);
126136
return false;
137+
}
127138

128-
if (EV_CURRENT != elf_hdr->e_ident[EI_VERSION])
139+
if (EV_CURRENT != elf_hdr->e_ident[EI_VERSION]) {
140+
SE_TRACE(SE_TRACE_ERROR, "Expected EV_CURRENT: 0x%x\n",
141+
elf_hdr->e_ident[EI_VERSION]);
129142
return false;
143+
}
130144

131-
if (ET_DYN != elf_hdr->e_type)
145+
if (ET_DYN != elf_hdr->e_type) {
146+
SE_TRACE(SE_TRACE_ERROR, "Expected ET_DYN: 0x%x\n",
147+
elf_hdr->e_type);
132148
return false;
149+
}
133150

134-
if (sizeof(ElfW(Phdr)) != elf_hdr->e_phentsize)
151+
if (sizeof(ElfW(Phdr)) != elf_hdr->e_phentsize) {
152+
SE_TRACE(SE_TRACE_ERROR, "Expected phentsize == %d, got %d\n",
153+
sizeof(ElfW(Phdr)),
154+
elf_hdr->e_phentsize);
135155
return false;
156+
}
136157

137158
return true;
138159
}
@@ -608,45 +629,60 @@ sgx_status_t ElfParser::run_parser()
608629
if (m_sections.size() != 0) return SGX_SUCCESS;
609630

610631
const ElfW(Ehdr) *elf_hdr = (const ElfW(Ehdr) *)m_start_addr;
611-
if (elf_hdr == NULL || m_len < sizeof(ElfW(Ehdr)))
632+
if (elf_hdr == NULL || m_len < sizeof(ElfW(Ehdr))) {
633+
SE_TRACE_ERROR("Header invalid size\n");
612634
return SGX_ERROR_INVALID_ENCLAVE;
613-
635+
}
614636
/* Check elf header*/
615-
if (!validate_elf_header(elf_hdr))
637+
if (!validate_elf_header(elf_hdr)) {
638+
SE_TRACE_ERROR("Header invalid\n");
616639
return SGX_ERROR_INVALID_ENCLAVE;
617-
640+
}
618641
/* Get and check machine mode */
619-
if (!get_bin_fmt(elf_hdr, m_bin_fmt))
642+
if (!get_bin_fmt(elf_hdr, m_bin_fmt)) {
643+
SE_TRACE_ERROR("Bin fmt incorrect\n");
620644
return SGX_ERROR_MODE_INCOMPATIBLE;
645+
}
621646

622647
/* Check if there is any overlap segment, and make sure the segment is 1 page aligned;
623648
* TLS segment must exist.
624649
*/
625-
if (!validate_segment(elf_hdr, m_len))
650+
if (!validate_segment(elf_hdr, m_len)) {
651+
SE_TRACE_ERROR("Segment incorrect\n");
626652
return SGX_ERROR_INVALID_ENCLAVE;
653+
}
627654

628-
if (!parse_dyn(elf_hdr, &m_dyn_info[0]))
655+
if (!parse_dyn(elf_hdr, &m_dyn_info[0])) {
656+
SE_TRACE_ERROR("Dyn incorrect\n");
629657
return SGX_ERROR_INVALID_ENCLAVE;
658+
}
630659

631660
/* Check if there is any undefined symbol */
632661
if (!check_symbol_table(elf_hdr, m_dyn_info, m_sym_table))
633662
{
663+
SE_TRACE_ERROR("Symbol table incorrect\n");
634664
return SGX_ERROR_UNDEFINED_SYMBOL;
635665
}
636666

637667
/* Check if there is unexpected relocation type */
638-
if (!validate_reltabs(elf_hdr, m_dyn_info))
668+
if (!validate_reltabs(elf_hdr, m_dyn_info)) {
669+
SE_TRACE_ERROR("Reltabs incorrect\n");
639670
return SGX_ERROR_INVALID_ENCLAVE;
671+
}
640672

641673
/* Check if there is .ctor section */
642-
if (has_ctor_section(elf_hdr))
674+
if (has_ctor_section(elf_hdr)) {
675+
SE_TRACE_ERROR("ctor section incorrect\n");
643676
return SGX_ERROR_INVALID_ENCLAVE;
677+
}
644678

645679
/* build regular sections */
646680
if (build_regular_sections(m_start_addr, m_sections, m_tls_section, m_metadata_offset, m_metadata_block_size))
647681
return SGX_SUCCESS;
648-
else
682+
else {
683+
SE_TRACE_ERROR("Regular sections incorrect\n");
649684
return SGX_ERROR_INVALID_ENCLAVE;
685+
}
650686
}
651687

652688
ElfParser::~ElfParser()

sdk/sign_tool/SignTool/manage_metadata.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ bool CMetadata::update_layout_entries()
514514
if(m_rva == 0)
515515
{
516516
se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
517+
se_trace(SE_TRACE_ERROR, "Sections size is 0\n");
517518
return false;
518519
}
519520

@@ -616,7 +617,8 @@ bool CMetadata::build_layout_entries()
616617
layout_t *layout_table = (layout_t *) alloc_buffer_from_metadata(size);
617618
if(layout_table == NULL)
618619
{
619-
se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
620+
se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
621+
se_trace(SE_TRACE_ERROR, "Layout table could not be allocated\n");
620622
return false;
621623
}
622624
m_metadata->dirs[DIR_LAYOUT].offset = (uint32_t)PTR_DIFF(layout_table, m_metadata);
@@ -858,7 +860,8 @@ bool CMetadata::build_layout_table()
858860
// tcs template
859861
if(false == build_tcs_template(tcs_template))
860862
{
861-
se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
863+
se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
864+
se_trace(SE_TRACE_ERROR, "Could not build TCS template\n");
862865
return false;
863866
}
864867
return true;
@@ -870,7 +873,8 @@ bool CMetadata::build_patch_entries(std::vector<patch_entry_t> &patches)
870873
patch_entry_t *patch_table = (patch_entry_t *) alloc_buffer_from_metadata(size);
871874
if(patch_table == NULL)
872875
{
873-
se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
876+
se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
877+
se_trace(SE_TRACE_ERROR, "Could not allocate patch table");
874878
return false;
875879
}
876880
m_metadata->dirs[DIR_PATCH].offset = (uint32_t)PTR_DIFF(patch_table, m_metadata);
@@ -901,7 +905,8 @@ bool CMetadata::build_patch_table()
901905
uint64_t rva = m_parser->get_symbol_rva("g_global_data");
902906
if(0 == rva)
903907
{
904-
se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
908+
se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
909+
se_trace(SE_TRACE_ERROR, "Could not find g_global_data\n");
905910
return false;
906911
}
907912

@@ -915,7 +920,8 @@ bool CMetadata::build_patch_table()
915920
uint8_t *zero = (uint8_t *)alloc_buffer_from_metadata(0); // get addr only, size will be determined later
916921
if(zero == NULL)
917922
{
918-
se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
923+
se_trace(SE_TRACE_ERROR, INVALID_ENCLAVE_ERROR);
924+
se_trace(SE_TRACE_ERROR, "Could not allocate 0 bytes\n");
919925
return false;
920926
}
921927
bin_fmt_t bf = m_parser->get_bin_format();

0 commit comments

Comments
 (0)