Skip to content

Commit 78eb715

Browse files
keesrustyrussell
authored andcommitted
kallsyms: generalize address range checking
This refactors the address range checks to be generalized instead of specific to text range checks, in preparation for other range checks. Also extracts logic for "is the symbol absolute" into a function. Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 0283f9a commit 78eb715

1 file changed

Lines changed: 32 additions & 21 deletions

File tree

‎scripts/kallsyms.c‎

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ struct sym_entry {
3636
unsigned char *sym;
3737
};
3838

39-
struct text_range {
40-
const char *stext, *etext;
39+
struct addr_range {
40+
const char *start_sym, *end_sym;
4141
unsigned long long start, end;
4242
};
4343

4444
static unsigned long long _text;
45-
static struct text_range text_ranges[] = {
45+
static struct addr_range text_ranges[] = {
4646
{ "_stext", "_etext" },
4747
{ "_sinittext", "_einittext" },
4848
{ "_stext_l1", "_etext_l1" }, /* Blackfin on-chip L1 inst SRAM */
@@ -83,19 +83,20 @@ static inline int is_arm_mapping_symbol(const char *str)
8383
&& (str[2] == '\0' || str[2] == '.');
8484
}
8585

86-
static int read_symbol_tr(const char *sym, unsigned long long addr)
86+
static int check_symbol_range(const char *sym, unsigned long long addr,
87+
struct addr_range *ranges, int entries)
8788
{
8889
size_t i;
89-
struct text_range *tr;
90+
struct addr_range *ar;
9091

91-
for (i = 0; i < ARRAY_SIZE(text_ranges); ++i) {
92-
tr = &text_ranges[i];
92+
for (i = 0; i < entries; ++i) {
93+
ar = &ranges[i];
9394

94-
if (strcmp(sym, tr->stext) == 0) {
95-
tr->start = addr;
95+
if (strcmp(sym, ar->start_sym) == 0) {
96+
ar->start = addr;
9697
return 0;
97-
} else if (strcmp(sym, tr->etext) == 0) {
98-
tr->end = addr;
98+
} else if (strcmp(sym, ar->end_sym) == 0) {
99+
ar->end = addr;
99100
return 0;
100101
}
101102
}
@@ -130,7 +131,8 @@ static int read_symbol(FILE *in, struct sym_entry *s)
130131
/* Ignore most absolute/undefined (?) symbols. */
131132
if (strcmp(sym, "_text") == 0)
132133
_text = s->addr;
133-
else if (read_symbol_tr(sym, s->addr) == 0)
134+
else if (check_symbol_range(sym, s->addr, text_ranges,
135+
ARRAY_SIZE(text_ranges)) == 0)
134136
/* nothing to do */;
135137
else if (toupper(stype) == 'A')
136138
{
@@ -167,15 +169,16 @@ static int read_symbol(FILE *in, struct sym_entry *s)
167169
return 0;
168170
}
169171

170-
static int symbol_valid_tr(struct sym_entry *s)
172+
static int symbol_in_range(struct sym_entry *s, struct addr_range *ranges,
173+
int entries)
171174
{
172175
size_t i;
173-
struct text_range *tr;
176+
struct addr_range *ar;
174177

175-
for (i = 0; i < ARRAY_SIZE(text_ranges); ++i) {
176-
tr = &text_ranges[i];
178+
for (i = 0; i < entries; ++i) {
179+
ar = &ranges[i];
177180

178-
if (s->addr >= tr->start && s->addr <= tr->end)
181+
if (s->addr >= ar->start && s->addr <= ar->end)
179182
return 1;
180183
}
181184

@@ -214,7 +217,8 @@ static int symbol_valid(struct sym_entry *s)
214217
/* if --all-symbols is not specified, then symbols outside the text
215218
* and inittext sections are discarded */
216219
if (!all_symbols) {
217-
if (symbol_valid_tr(s) == 0)
220+
if (symbol_in_range(s, text_ranges,
221+
ARRAY_SIZE(text_ranges)) == 0)
218222
return 0;
219223
/* Corner case. Discard any symbols with the same value as
220224
* _etext _einittext; they can move between pass 1 and 2 when
@@ -223,9 +227,11 @@ static int symbol_valid(struct sym_entry *s)
223227
* rules.
224228
*/
225229
if ((s->addr == text_range_text->end &&
226-
strcmp((char *)s->sym + offset, text_range_text->etext)) ||
230+
strcmp((char *)s->sym + offset,
231+
text_range_text->end_sym)) ||
227232
(s->addr == text_range_inittext->end &&
228-
strcmp((char *)s->sym + offset, text_range_inittext->etext)))
233+
strcmp((char *)s->sym + offset,
234+
text_range_inittext->end_sym)))
229235
return 0;
230236
}
231237

@@ -298,6 +304,11 @@ static int expand_symbol(unsigned char *data, int len, char *result)
298304
return total;
299305
}
300306

307+
static int symbol_absolute(struct sym_entry *s)
308+
{
309+
return toupper(s->sym[0]) == 'A';
310+
}
311+
301312
static void write_src(void)
302313
{
303314
unsigned int i, k, off;
@@ -325,7 +336,7 @@ static void write_src(void)
325336
*/
326337
output_label("kallsyms_addresses");
327338
for (i = 0; i < table_cnt; i++) {
328-
if (toupper(table[i].sym[0]) != 'A') {
339+
if (!symbol_absolute(&table[i])) {
329340
if (_text <= table[i].addr)
330341
printf("\tPTR\t_text + %#llx\n",
331342
table[i].addr - _text);

0 commit comments

Comments
 (0)