Changeset 40eab9f in mainline for kernel/generic/src/debug/sections.c
- Timestamp:
- 2023-11-03T18:47:41Z (15 months ago)
- Branches:
- master, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- b1397ab
- Parents:
- dcd8214
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2023-11-03 18:46:22)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2023-11-03 18:47:41)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/debug/sections.c
rdcd8214 r40eab9f 29 29 #include <debug/sections.h> 30 30 #include <debug/register.h> 31 #include <debug.h> 31 32 32 33 #include <stdio.h> 33 34 #include <str.h> 34 35 35 const void *debug_aranges = NULL; 36 size_t debug_aranges_size = 0; 36 #define DEBUGF dummy_printf 37 37 38 const void *debug_info = NULL; 39 size_t debug_info_size = 0; 40 41 const void *debug_abbrev = NULL; 42 size_t debug_abbrev_size = 0; 43 44 const void *debug_line = NULL; 45 size_t debug_line_size = 0; 46 47 const char *debug_str = NULL; 48 size_t debug_str_size = 0; 49 50 const char *debug_line_str = NULL; 51 size_t debug_line_str_size = 0; 52 53 const void *debug_rnglists = NULL; 54 size_t debug_rnglists_size = 0; 55 56 // TODO: get this from the program image 57 const void *eh_frame_hdr = NULL; 58 size_t eh_frame_hdr_size = 0; 59 60 const void *eh_frame = NULL; 61 size_t eh_frame_size = 0; 62 63 const elf_symbol_t *symtab = NULL; 64 size_t symtab_size = 0; 65 66 const char *strtab = NULL; 67 size_t strtab_size = 0; 38 debug_sections_t kernel_sections; 68 39 69 40 struct section_manifest { … … 73 44 }; 74 45 75 #define section(name) { "." #name, (const void **)&name, &name##_size } 46 static void _check_string_section(const char *section, size_t *section_size) 47 { 48 while (*section_size > 0 && section[*section_size - 1] != 0) { 49 (*section_size)--; 50 } 51 } 76 52 77 static const struct section_manifest manifest[] = { 78 section(debug_aranges), 79 section(debug_info), 80 section(debug_abbrev), 81 section(debug_line), 82 section(debug_str), 83 section(debug_line_str), 84 section(debug_rnglists), 85 section(eh_frame_hdr), 86 section(eh_frame), 87 section(symtab), 88 section(strtab), 89 }; 53 debug_sections_t get_debug_sections(const void *elf, size_t elf_size) 54 { 55 debug_sections_t out = { }; 90 56 91 # undef section57 #define section(name) { "." #name, (const void **)&out.name, &out.name##_size } 92 58 93 static const size_t manifest_len = sizeof(manifest) / sizeof(struct section_manifest); 59 const struct section_manifest manifest[] = { 60 section(debug_aranges), 61 section(debug_info), 62 section(debug_abbrev), 63 section(debug_line), 64 section(debug_str), 65 section(debug_line_str), 66 section(debug_rnglists), 67 section(eh_frame_hdr), 68 section(eh_frame), 69 section(symtab), 70 section(strtab), 71 }; 94 72 95 void register_debug_data(const void *data, size_t data_size) 96 { 97 const void *data_end = data + data_size;73 const size_t manifest_len = sizeof(manifest) / sizeof(struct section_manifest); 74 75 const void *data_end = elf + elf_size; 98 76 99 77 /* … … 103 81 */ 104 82 105 if ( data_size < sizeof(elf_header_t)) {83 if (elf_size < sizeof(elf_header_t)) { 106 84 printf("bad debug data: too short\n"); 107 return ;85 return out; 108 86 } 109 87 110 if (((uintptr_t) data) % 8 != 0) {88 if (((uintptr_t) elf) % 8 != 0) { 111 89 printf("bad debug data: unaligned input\n"); 112 return ;90 return out; 113 91 } 114 92 115 const elf_header_t *hdr = data;93 const elf_header_t *hdr = elf; 116 94 117 95 if (hdr->e_ident[0] != ELFMAG0 || hdr->e_ident[1] != ELFMAG1 || 118 96 hdr->e_ident[2] != ELFMAG2 || hdr->e_ident[3] != ELFMAG3) { 119 97 printf("bad debug data: wrong ELF magic bytes\n"); 120 return ;98 return out; 121 99 } 122 100 123 101 if (hdr->e_shentsize != sizeof(elf_section_header_t)) { 124 102 printf("bad debug data: wrong e_shentsize\n"); 125 return ;103 return out; 126 104 } 127 105 128 106 /* Get section header table. */ 129 const elf_section_header_t *shdr = data+ hdr->e_shoff;107 const elf_section_header_t *shdr = elf + hdr->e_shoff; 130 108 size_t shdr_len = hdr->e_shnum; 131 109 132 110 if ((void *) &shdr[shdr_len] > data_end) { 133 111 printf("bad debug data: truncated section header table\n"); 134 return ;112 return out; 135 113 } 136 114 137 115 if (hdr->e_shstrndx >= shdr_len) { 138 116 printf("bad debug data: string table index out of range\n"); 139 return ;117 return out; 140 118 } 141 119 142 120 /* Get data on section name string table. */ 143 121 const elf_section_header_t *shstr = &shdr[hdr->e_shstrndx]; 144 const char *shstrtab = data+ shstr->sh_offset;122 const char *shstrtab = elf + shstr->sh_offset; 145 123 size_t shstrtab_size = shstr->sh_size; 146 124 147 125 if ((void *) shstrtab + shstrtab_size > data_end) { 148 126 printf("bad debug data: truncated string table\n"); 149 return ;127 return out; 150 128 } 151 129 152 130 /* Check NUL-terminator. */ 153 while (shstrtab_size > 0 && shstrtab[shstrtab_size - 1] != 0) { 154 shstrtab_size--; 155 } 131 _check_string_section(shstrtab, &shstrtab_size); 156 132 157 133 if (shstrtab_size == 0) { 158 134 printf("bad debug data: empty or non-null-terminated string table\n"); 159 return ;135 return out; 160 136 } 161 137 … … 167 143 if (shdr[i].sh_name >= shstrtab_size) { 168 144 printf("bad debug data: string table index out of range\n"); 169 return;145 continue; 170 146 } 171 147 … … 174 150 size_t size = shdr[i].sh_size; 175 151 176 printf("section '%s': offset %zd (%zd bytes)\n", name, offset, size);152 DEBUGF("section '%s': offset %zd (%zd bytes)\n", name, offset, size); 177 153 178 if ( data+ offset + size > data_end) {154 if (elf + offset + size > data_end) { 179 155 printf("bad debug data: truncated section %s\n", name); 180 156 continue; … … 183 159 for (size_t i = 0; i < manifest_len; i++) { 184 160 if (str_cmp(manifest[i].name, name) == 0) { 185 *manifest[i].address_field = data+ offset;161 *manifest[i].address_field = elf + offset; 186 162 *manifest[i].size_field = size; 187 163 break; … … 191 167 192 168 /* Check NUL-terminator in .strtab, .debug_str and .debug_line_str */ 193 while (strtab_size > 0 && strtab[strtab_size - 1] != 0) {194 strtab_size--;195 }169 _check_string_section(out.strtab, &out.strtab_size); 170 _check_string_section(out.debug_str, &out.debug_str_size); 171 _check_string_section(out.debug_line_str, &out.debug_line_str_size); 196 172 197 while (debug_str_size > 0 && debug_str[debug_str_size - 1] != 0) { 198 debug_str_size--; 199 } 173 return out; 174 } 200 175 201 while (debug_line_str_size > 0 && debug_str[debug_line_str_size - 1] != 0) { 202 debug_line_str_size--; 203 }176 void register_debug_data(const void *elf, size_t elf_size) 177 { 178 kernel_sections = get_debug_sections(elf, elf_size); 204 179 }
Note:
See TracChangeset
for help on using the changeset viewer.