1
From e673ca50127b6c1263727aa31de0b8bb966ca7a2 Mon Sep 17 00:00:00 2001
2
From: Ian Jackson <ian.jackson@eu.citrix.com>
3
Date: Fri, 14 Jun 2013 16:43:18 +0100
4
Subject: [PATCH 15/23] libelf: use only unsigned integers
6
Signed integers have undesirable undefined behaviours on overflow.
7
Malicious compilers can turn apparently-correct code into code with
8
security vulnerabilities etc.
10
So use only unsigned integers. Exceptions are booleans (which we have
11
already changed) and error codes.
13
We _do_ change all the chars which aren't fixed constants from our own
14
text segment, but not the char*s. This is because it is safe to
15
access an arbitrary byte through a char*, but not necessarily safe to
16
convert an arbitrary value to a char.
18
As a consequence we need to compile libelf with -Wno-pointer-sign.
20
It is OK to change all the signed integers to unsigned because all the
21
inequalities in libelf are in contexts where we don't "expect"
24
In libelf-dominfo.c:elf_xen_parse we rename a variable "rc" to
25
"more_notes" as it actually contains a note count derived from the
26
input image. The "error" return value from elf_xen_parse_notes is
27
changed from -1 to ~0U.
29
grepping shows only one occurrence of "PRId" or "%d" or "%ld" in
30
libelf and xc_dom_elfloader.c (a "%d" which becomes "%u").
32
This is part of the fix to a security issue, XSA-55.
34
For those concerned about unintentional functional changes, the
35
following rune produces a version of the patch which is much smaller
36
and eliminates only non-functional changes:
38
GIT_EXTERNAL_DIFF=.../unsigned-differ git-diff <before>..<after>
40
where <before> and <after> are git refs for the code before and after
41
this patch, and unsigned-differ is this shell script:
47
perl -pe 's/\b(?:elf_errorstatus|elf_negerrnoval)\b/int/g'
55
diff -pu --label "$path~" <(seddery <"$in") --label "$path" <(seddery <"$out")
58
if [ $rc = 1 ]; then rc=0; fi
61
Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
63
tools/libxc/Makefile | 9 +++++-
64
tools/libxc/xc_dom.h | 7 +++--
65
tools/libxc/xc_dom_elfloader.c | 42 ++++++++++++++++-------------
66
tools/xcutils/readnotes.c | 15 +++++-----
67
xen/common/libelf/Makefile | 2 +
68
xen/common/libelf/libelf-dominfo.c | 52 ++++++++++++++++++-----------------
69
xen/common/libelf/libelf-loader.c | 20 +++++++-------
70
xen/common/libelf/libelf-tools.c | 24 ++++++++--------
71
xen/include/xen/libelf.h | 21 ++++++++------
72
9 files changed, 105 insertions(+), 87 deletions(-)
74
diff --git a/tools/libxc/Makefile b/tools/libxc/Makefile
75
index d8c6a60..a3fd90c 100644
76
--- a/tools/libxc/Makefile
77
+++ b/tools/libxc/Makefile
78
@@ -52,8 +52,13 @@ endif
79
vpath %.c ../../xen/common/libelf
80
CFLAGS += -I../../xen/common/libelf
82
-GUEST_SRCS-y += libelf-tools.c libelf-loader.c
83
-GUEST_SRCS-y += libelf-dominfo.c
84
+ELF_SRCS-y += libelf-tools.c libelf-loader.c
85
+ELF_SRCS-y += libelf-dominfo.c
87
+GUEST_SRCS-y += $(ELF_SRCS-y)
89
+$(patsubst %.c,%.o,$(ELF_SRCS-y)): CFLAGS += -Wno-pointer-sign
90
+$(patsubst %.c,%.opic,$(ELF_SRCS-y)): CFLAGS += -Wno-pointer-sign
93
GUEST_SRCS-y += xc_dom_core.c xc_dom_boot.c
94
diff --git a/tools/libxc/xc_dom.h b/tools/libxc/xc_dom.h
95
index 9f8037e..0161459 100644
96
--- a/tools/libxc/xc_dom.h
97
+++ b/tools/libxc/xc_dom.h
98
@@ -140,9 +140,10 @@ struct xc_dom_image {
100
struct xc_dom_loader {
102
- int (*probe) (struct xc_dom_image * dom);
103
- int (*parser) (struct xc_dom_image * dom);
104
- int (*loader) (struct xc_dom_image * dom);
105
+ /* Sadly the error returns from these functions are not consistent: */
106
+ elf_negerrnoval (*probe) (struct xc_dom_image * dom);
107
+ elf_negerrnoval (*parser) (struct xc_dom_image * dom);
108
+ elf_errorstatus (*loader) (struct xc_dom_image * dom);
110
struct xc_dom_loader *next;
112
diff --git a/tools/libxc/xc_dom_elfloader.c b/tools/libxc/xc_dom_elfloader.c
113
index 9ba64ae..62a0d3b 100644
114
--- a/tools/libxc/xc_dom_elfloader.c
115
+++ b/tools/libxc/xc_dom_elfloader.c
116
@@ -84,7 +84,7 @@ static char *xc_dom_guest_type(struct xc_dom_image *dom,
117
/* ------------------------------------------------------------------------ */
118
/* parse elf binary */
120
-static int check_elf_kernel(struct xc_dom_image *dom, bool verbose)
121
+static elf_negerrnoval check_elf_kernel(struct xc_dom_image *dom, bool verbose)
123
if ( dom->kernel_blob == NULL )
125
@@ -106,12 +106,12 @@ static int check_elf_kernel(struct xc_dom_image *dom, bool verbose)
129
-static int xc_dom_probe_elf_kernel(struct xc_dom_image *dom)
130
+static elf_negerrnoval xc_dom_probe_elf_kernel(struct xc_dom_image *dom)
132
return check_elf_kernel(dom, 0);
135
-static int xc_dom_load_elf_symtab(struct xc_dom_image *dom,
136
+static elf_errorstatus xc_dom_load_elf_symtab(struct xc_dom_image *dom,
137
struct elf_binary *elf, bool load)
139
struct elf_binary syms;
140
@@ -119,7 +119,7 @@ static int xc_dom_load_elf_symtab(struct xc_dom_image *dom,
141
xen_vaddr_t symtab, maxaddr;
144
- int h, count, type, i, tables = 0;
145
+ unsigned h, count, type, i, tables = 0;
149
@@ -140,13 +140,13 @@ static int xc_dom_load_elf_symtab(struct xc_dom_image *dom,
150
elf->caller_xdest_base = hdr_ptr;
151
elf->caller_xdest_size = allow_size;
152
hdr = ELF_REALPTR2PTRVAL(hdr_ptr);
153
- elf_store_val(elf, int, hdr, size - sizeof(int));
154
+ elf_store_val(elf, unsigned, hdr, size - sizeof(unsigned));
160
- size = sizeof(int) + elf_size(elf, elf->ehdr) +
161
+ size = sizeof(unsigned) + elf_size(elf, elf->ehdr) +
162
elf_shdr_count(elf) * elf_size(elf, shdr);
163
hdr_ptr = xc_dom_malloc(dom, size);
164
if ( hdr_ptr == NULL )
165
@@ -157,15 +157,15 @@ static int xc_dom_load_elf_symtab(struct xc_dom_image *dom,
166
dom->bsd_symtab_start = elf_round_up(elf, dom->kernel_seg.vend);
169
- elf_memcpy_safe(elf, hdr + sizeof(int),
170
+ elf_memcpy_safe(elf, hdr + sizeof(unsigned),
172
elf_size(elf, elf->ehdr));
173
- elf_memcpy_safe(elf, hdr + sizeof(int) + elf_size(elf, elf->ehdr),
174
+ elf_memcpy_safe(elf, hdr + sizeof(unsigned) + elf_size(elf, elf->ehdr),
175
ELF_IMAGE_BASE(elf) + elf_uval(elf, elf->ehdr, e_shoff),
176
elf_shdr_count(elf) * elf_size(elf, shdr));
177
if ( elf_64bit(elf) )
179
- Elf64_Ehdr *ehdr = (Elf64_Ehdr *)(hdr + sizeof(int));
180
+ Elf64_Ehdr *ehdr = (Elf64_Ehdr *)(hdr + sizeof(unsigned));
182
ehdr->e_phentsize = 0;
184
@@ -174,22 +174,22 @@ static int xc_dom_load_elf_symtab(struct xc_dom_image *dom,
188
- Elf32_Ehdr *ehdr = (Elf32_Ehdr *)(hdr + sizeof(int));
189
+ Elf32_Ehdr *ehdr = (Elf32_Ehdr *)(hdr + sizeof(unsigned));
191
ehdr->e_phentsize = 0;
193
ehdr->e_shoff = elf_size(elf, elf->ehdr);
194
ehdr->e_shstrndx = SHN_UNDEF;
196
- if ( elf->caller_xdest_size < sizeof(int) )
197
+ if ( elf->caller_xdest_size < sizeof(unsigned) )
199
DOMPRINTF("%s/%s: header size %"PRIx64" too small",
200
__FUNCTION__, load ? "load" : "parse",
201
(uint64_t)elf->caller_xdest_size);
204
- if ( elf_init(&syms, elf->caller_xdest_base + sizeof(int),
205
- elf->caller_xdest_size - sizeof(int)) )
206
+ if ( elf_init(&syms, elf->caller_xdest_base + sizeof(unsigned),
207
+ elf->caller_xdest_size - sizeof(unsigned)) )
211
@@ -209,7 +209,7 @@ static int xc_dom_load_elf_symtab(struct xc_dom_image *dom,
213
xc_elf_set_logfile(dom->xch, &syms, 1);
215
- symtab = dom->bsd_symtab_start + sizeof(int);
216
+ symtab = dom->bsd_symtab_start + sizeof(unsigned);
217
maxaddr = elf_round_up(&syms, symtab + elf_size(&syms, syms.ehdr) +
218
elf_shdr_count(&syms) * elf_size(&syms, shdr));
220
@@ -255,7 +255,7 @@ static int xc_dom_load_elf_symtab(struct xc_dom_image *dom,
221
size = elf_uval(&syms, shdr, sh_size);
222
maxaddr = elf_round_up(&syms, maxaddr + size);
224
- DOMPRINTF("%s: h=%d %s, size=0x%zx, maxaddr=0x%" PRIx64 "",
225
+ DOMPRINTF("%s: h=%u %s, size=0x%zx, maxaddr=0x%" PRIx64 "",
227
type == SHT_SYMTAB ? "symtab" : "strtab",
229
@@ -294,10 +294,14 @@ static int xc_dom_load_elf_symtab(struct xc_dom_image *dom,
233
-static int xc_dom_parse_elf_kernel(struct xc_dom_image *dom)
234
+static elf_errorstatus xc_dom_parse_elf_kernel(struct xc_dom_image *dom)
236
+ * This function sometimes returns -1 for error and sometimes
237
+ * an errno value. ?!?!
240
struct elf_binary *elf;
242
+ elf_errorstatus rc;
244
rc = check_elf_kernel(dom, 1);
246
@@ -358,10 +362,10 @@ out:
250
-static int xc_dom_load_elf_kernel(struct xc_dom_image *dom)
251
+static elf_errorstatus xc_dom_load_elf_kernel(struct xc_dom_image *dom)
253
struct elf_binary *elf = dom->private_loader;
255
+ elf_errorstatus rc;
258
elf->dest_base = xc_dom_seg_to_ptr_pages(dom, &dom->kernel_seg, &pages);
259
diff --git a/tools/xcutils/readnotes.c b/tools/xcutils/readnotes.c
260
index d1f7a30..2ca7732 100644
261
--- a/tools/xcutils/readnotes.c
262
+++ b/tools/xcutils/readnotes.c
263
@@ -70,7 +70,7 @@ static void print_numeric_note(const char *prefix, struct elf_binary *elf,
264
ELF_HANDLE_DECL(elf_note) note)
266
uint64_t value = elf_note_numeric(elf, note);
267
- int descsz = elf_uval(elf, note, descsz);
268
+ unsigned descsz = elf_uval(elf, note, descsz);
270
printf("%s: %#*" PRIx64 " (%d bytes)\n",
271
prefix, 2+2*descsz, value, descsz);
272
@@ -79,7 +79,7 @@ static void print_numeric_note(const char *prefix, struct elf_binary *elf,
273
static void print_l1_mfn_valid_note(const char *prefix, struct elf_binary *elf,
274
ELF_HANDLE_DECL(elf_note) note)
276
- int descsz = elf_uval(elf, note, descsz);
277
+ unsigned descsz = elf_uval(elf, note, descsz);
278
ELF_PTRVAL_CONST_VOID desc = elf_note_desc(elf, note);
280
/* XXX should be able to cope with a list of values. */
281
@@ -99,10 +99,10 @@ static void print_l1_mfn_valid_note(const char *prefix, struct elf_binary *elf,
285
-static int print_notes(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) start, ELF_HANDLE_DECL(elf_note) end)
286
+static unsigned print_notes(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) start, ELF_HANDLE_DECL(elf_note) end)
288
ELF_HANDLE_DECL(elf_note) note;
289
- int notes_found = 0;
290
+ unsigned notes_found = 0;
291
const char *this_note_name;
293
for ( note = start; ELF_HANDLE_PTRVAL(note) < ELF_HANDLE_PTRVAL(end); note = elf_note_next(elf, note) )
294
@@ -161,7 +161,7 @@ static int print_notes(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) start,
297
printf("unknown note type %#x\n",
298
- (int)elf_uval(elf, note, type));
299
+ (unsigned)elf_uval(elf, note, type));
303
@@ -171,12 +171,13 @@ static int print_notes(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) start,
304
int main(int argc, char **argv)
307
- int fd,h,size,usize,count;
309
+ unsigned h,size,usize,count;
312
struct elf_binary elf;
313
ELF_HANDLE_DECL(elf_shdr) shdr;
314
- int notes_found = 0;
315
+ unsigned notes_found = 0;
317
struct setup_header *hdr;
318
uint64_t payload_offset, payload_length;
319
diff --git a/xen/common/libelf/Makefile b/xen/common/libelf/Makefile
320
index 18dc8e2..5bf8f76 100644
321
--- a/xen/common/libelf/Makefile
322
+++ b/xen/common/libelf/Makefile
323
@@ -2,6 +2,8 @@ obj-bin-y := libelf.o
325
SECTIONS := text data $(SPECIAL_DATA_SECTIONS)
327
+CFLAGS += -Wno-pointer-sign
329
libelf.o: libelf-temp.o Makefile
330
$(OBJCOPY) $(foreach s,$(SECTIONS),--rename-section .$(s)=.init.$(s)) $< $@
332
diff --git a/xen/common/libelf/libelf-dominfo.c b/xen/common/libelf/libelf-dominfo.c
333
index 12b6c2a..cdd0d31 100644
334
--- a/xen/common/libelf/libelf-dominfo.c
335
+++ b/xen/common/libelf/libelf-dominfo.c
336
@@ -29,15 +29,15 @@ static const char *const elf_xen_feature_names[] = {
337
[XENFEAT_pae_pgdir_above_4gb] = "pae_pgdir_above_4gb",
338
[XENFEAT_dom0] = "dom0"
340
-static const int elf_xen_features =
341
+static const unsigned elf_xen_features =
342
sizeof(elf_xen_feature_names) / sizeof(elf_xen_feature_names[0]);
344
-int elf_xen_parse_features(const char *features,
345
+elf_errorstatus elf_xen_parse_features(const char *features,
351
+ unsigned char feature[64];
352
+ unsigned pos, len, i;
354
if ( features == NULL )
356
@@ -94,7 +94,7 @@ int elf_xen_parse_features(const char *features,
357
/* ------------------------------------------------------------------------ */
360
-int elf_xen_parse_note(struct elf_binary *elf,
361
+elf_errorstatus elf_xen_parse_note(struct elf_binary *elf,
362
struct elf_dom_parms *parms,
363
ELF_HANDLE_DECL(elf_note) note)
365
@@ -125,7 +125,7 @@ int elf_xen_parse_note(struct elf_binary *elf,
366
const char *str = NULL;
369
- int type = elf_uval(elf, note, type);
370
+ unsigned type = elf_uval(elf, note, type);
372
if ( (type >= sizeof(note_desc) / sizeof(note_desc[0])) ||
373
(note_desc[type].name == NULL) )
374
@@ -216,12 +216,14 @@ int elf_xen_parse_note(struct elf_binary *elf,
378
-static int elf_xen_parse_notes(struct elf_binary *elf,
379
+#define ELF_NOTE_INVALID (~0U)
381
+static unsigned elf_xen_parse_notes(struct elf_binary *elf,
382
struct elf_dom_parms *parms,
383
ELF_PTRVAL_CONST_VOID start,
384
ELF_PTRVAL_CONST_VOID end)
386
- int xen_elfnotes = 0;
387
+ unsigned xen_elfnotes = 0;
388
ELF_HANDLE_DECL(elf_note) note;
389
const char *note_name;
391
@@ -237,7 +239,7 @@ static int elf_xen_parse_notes(struct elf_binary *elf,
392
if ( strcmp(note_name, "Xen") )
394
if ( elf_xen_parse_note(elf, parms, note) )
396
+ return ELF_NOTE_INVALID;
400
@@ -246,12 +248,12 @@ static int elf_xen_parse_notes(struct elf_binary *elf,
401
/* ------------------------------------------------------------------------ */
402
/* __xen_guest section */
404
-int elf_xen_parse_guest_info(struct elf_binary *elf,
405
+elf_errorstatus elf_xen_parse_guest_info(struct elf_binary *elf,
406
struct elf_dom_parms *parms)
408
ELF_PTRVAL_CONST_CHAR h;
409
- char name[32], value[128];
411
+ unsigned char name[32], value[128];
414
h = parms->guest_info;
415
#define STAR(h) (elf_access_unsigned(elf, (h), 0, 1))
416
@@ -334,13 +336,13 @@ int elf_xen_parse_guest_info(struct elf_binary *elf,
417
/* ------------------------------------------------------------------------ */
420
-static int elf_xen_note_check(struct elf_binary *elf,
421
+static elf_errorstatus elf_xen_note_check(struct elf_binary *elf,
422
struct elf_dom_parms *parms)
424
if ( (ELF_PTRVAL_INVALID(parms->elf_note_start)) &&
425
(ELF_PTRVAL_INVALID(parms->guest_info)) )
427
- int machine = elf_uval(elf, elf->ehdr, e_machine);
428
+ unsigned machine = elf_uval(elf, elf->ehdr, e_machine);
429
if ( (machine == EM_386) || (machine == EM_X86_64) )
431
elf_err(elf, "%s: ERROR: Not a Xen-ELF image: "
432
@@ -378,7 +380,7 @@ static int elf_xen_note_check(struct elf_binary *elf,
436
-static int elf_xen_addr_calc_check(struct elf_binary *elf,
437
+static elf_errorstatus elf_xen_addr_calc_check(struct elf_binary *elf,
438
struct elf_dom_parms *parms)
440
if ( (parms->elf_paddr_offset != UNSET_ADDR) &&
441
@@ -464,13 +466,13 @@ static int elf_xen_addr_calc_check(struct elf_binary *elf,
442
/* ------------------------------------------------------------------------ */
443
/* glue it all together ... */
445
-int elf_xen_parse(struct elf_binary *elf,
446
+elf_errorstatus elf_xen_parse(struct elf_binary *elf,
447
struct elf_dom_parms *parms)
449
ELF_HANDLE_DECL(elf_shdr) shdr;
450
ELF_HANDLE_DECL(elf_phdr) phdr;
451
- int xen_elfnotes = 0;
453
+ unsigned xen_elfnotes = 0;
454
+ unsigned i, count, more_notes;
456
elf_memset_unchecked(parms, 0, sizeof(*parms));
457
parms->virt_base = UNSET_ADDR;
458
@@ -495,13 +497,13 @@ int elf_xen_parse(struct elf_binary *elf,
459
if (elf_uval(elf, phdr, p_offset) == 0)
462
- rc = elf_xen_parse_notes(elf, parms,
463
+ more_notes = elf_xen_parse_notes(elf, parms,
464
elf_segment_start(elf, phdr),
465
elf_segment_end(elf, phdr));
467
+ if ( more_notes == ELF_NOTE_INVALID )
470
- xen_elfnotes += rc;
471
+ xen_elfnotes += more_notes;
475
@@ -518,17 +520,17 @@ int elf_xen_parse(struct elf_binary *elf,
476
if ( elf_uval(elf, shdr, sh_type) != SHT_NOTE )
479
- rc = elf_xen_parse_notes(elf, parms,
480
+ more_notes = elf_xen_parse_notes(elf, parms,
481
elf_section_start(elf, shdr),
482
elf_section_end(elf, shdr));
485
+ if ( more_notes == ELF_NOTE_INVALID )
488
- if ( xen_elfnotes == 0 && rc > 0 )
489
+ if ( xen_elfnotes == 0 && more_notes > 0 )
490
elf_msg(elf, "%s: using notes from SHT_NOTE section\n", __FUNCTION__);
492
- xen_elfnotes += rc;
493
+ xen_elfnotes += more_notes;
497
diff --git a/xen/common/libelf/libelf-loader.c b/xen/common/libelf/libelf-loader.c
498
index 0dccd4d..c3a9e51 100644
499
--- a/xen/common/libelf/libelf-loader.c
500
+++ b/xen/common/libelf/libelf-loader.c
503
/* ------------------------------------------------------------------------ */
505
-int elf_init(struct elf_binary *elf, const char *image_input, size_t size)
506
+elf_errorstatus elf_init(struct elf_binary *elf, const char *image_input, size_t size)
508
ELF_HANDLE_DECL(elf_shdr) shdr;
509
uint64_t i, count, section, offset;
510
@@ -114,7 +114,7 @@ void elf_set_log(struct elf_binary *elf, elf_log_callback *log_callback,
511
elf->verbose = verbose;
514
-static int elf_load_image(struct elf_binary *elf,
515
+static elf_errorstatus elf_load_image(struct elf_binary *elf,
516
ELF_PTRVAL_VOID dst, ELF_PTRVAL_CONST_VOID src,
517
uint64_t filesz, uint64_t memsz)
519
@@ -129,9 +129,9 @@ void elf_set_verbose(struct elf_binary *elf)
523
-static int elf_load_image(struct elf_binary *elf, ELF_PTRVAL_VOID dst, ELF_PTRVAL_CONST_VOID src, uint64_t filesz, uint64_t memsz)
524
+static elf_errorstatus elf_load_image(struct elf_binary *elf, ELF_PTRVAL_VOID dst, ELF_PTRVAL_CONST_VOID src, uint64_t filesz, uint64_t memsz)
527
+ elf_errorstatus rc;
528
if ( filesz > ULONG_MAX || memsz > ULONG_MAX )
530
/* We trust the dom0 kernel image completely, so we don't care
531
@@ -151,7 +151,7 @@ void elf_parse_bsdsyms(struct elf_binary *elf, uint64_t pstart)
534
ELF_HANDLE_DECL(elf_shdr) shdr;
538
if ( !ELF_HANDLE_VALID(elf->sym_tab) )
540
@@ -187,7 +187,7 @@ static void elf_load_bsdsyms(struct elf_binary *elf)
541
ELF_PTRVAL_VOID symbase;
542
ELF_PTRVAL_VOID symtab_addr;
543
ELF_HANDLE_DECL_NONCONST(elf_shdr) shdr;
547
if ( !elf->bsd_symtab_pstart )
549
@@ -220,7 +220,7 @@ do { \
550
elf_memcpy_safe(elf, ELF_HANDLE_PTRVAL(shdr),
551
ELF_IMAGE_BASE(elf) + elf_uval(elf, elf->ehdr, e_shoff),
553
- maxva = ELF_OBSOLETE_VOIDP_CAST elf_round_up(elf, (long)maxva + sz);
554
+ maxva = ELF_OBSOLETE_VOIDP_CAST elf_round_up(elf, (unsigned long)maxva + sz);
556
for ( i = 0; i < elf_shdr_count(elf); i++ )
558
@@ -233,10 +233,10 @@ do { \
559
elf_memcpy_safe(elf, maxva, elf_section_start(elf, shdr), sz);
560
/* Mangled to be based on ELF header location. */
561
elf_hdr_elm(elf, shdr, sh_offset, maxva - symtab_addr);
562
- maxva = ELF_OBSOLETE_VOIDP_CAST elf_round_up(elf, (long)maxva + sz);
563
+ maxva = ELF_OBSOLETE_VOIDP_CAST elf_round_up(elf, (unsigned long)maxva + sz);
565
shdr = ELF_MAKE_HANDLE(elf_shdr, ELF_HANDLE_PTRVAL(shdr) +
566
- (long)elf_uval(elf, elf->ehdr, e_shentsize));
567
+ (unsigned long)elf_uval(elf, elf->ehdr, e_shentsize));
570
/* Write down the actual sym size. */
571
@@ -273,7 +273,7 @@ void elf_parse_binary(struct elf_binary *elf)
572
__FUNCTION__, elf->pstart, elf->pend);
575
-int elf_load_binary(struct elf_binary *elf)
576
+elf_errorstatus elf_load_binary(struct elf_binary *elf)
578
ELF_HANDLE_DECL(elf_phdr) phdr;
579
uint64_t i, count, paddr, offset, filesz, memsz;
580
diff --git a/xen/common/libelf/libelf-tools.c b/xen/common/libelf/libelf-tools.c
581
index fa58f76..46d4ab1 100644
582
--- a/xen/common/libelf/libelf-tools.c
583
+++ b/xen/common/libelf/libelf-tools.c
584
@@ -122,19 +122,19 @@ uint64_t elf_access_unsigned(struct elf_binary * elf, elf_ptrval base,
586
uint64_t elf_round_up(struct elf_binary *elf, uint64_t addr)
588
- int elf_round = (elf_64bit(elf) ? 8 : 4) - 1;
589
+ uint64_t elf_round = (elf_64bit(elf) ? 8 : 4) - 1;
591
return (addr + elf_round) & ~elf_round;
594
/* ------------------------------------------------------------------------ */
596
-int elf_shdr_count(struct elf_binary *elf)
597
+unsigned elf_shdr_count(struct elf_binary *elf)
599
return elf_uval(elf, elf->ehdr, e_shnum);
602
-int elf_phdr_count(struct elf_binary *elf)
603
+unsigned elf_phdr_count(struct elf_binary *elf)
605
return elf_uval(elf, elf->ehdr, e_phnum);
607
@@ -144,7 +144,7 @@ ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_name(struct elf_binary *elf, const char *n
608
uint64_t count = elf_shdr_count(elf);
609
ELF_HANDLE_DECL(elf_shdr) shdr;
614
for ( i = 0; i < count; i++ )
616
@@ -156,7 +156,7 @@ ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_name(struct elf_binary *elf, const char *n
617
return ELF_INVALID_HANDLE(elf_shdr);
620
-ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_index(struct elf_binary *elf, int index)
621
+ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_index(struct elf_binary *elf, unsigned index)
623
uint64_t count = elf_shdr_count(elf);
624
ELF_PTRVAL_CONST_VOID ptr;
625
@@ -170,7 +170,7 @@ ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_index(struct elf_binary *elf, int index)
626
return ELF_MAKE_HANDLE(elf_shdr, ptr);
629
-ELF_HANDLE_DECL(elf_phdr) elf_phdr_by_index(struct elf_binary *elf, int index)
630
+ELF_HANDLE_DECL(elf_phdr) elf_phdr_by_index(struct elf_binary *elf, unsigned index)
632
uint64_t count = elf_uval(elf, elf->ehdr, e_phnum);
633
ELF_PTRVAL_CONST_VOID ptr;
634
@@ -264,7 +264,7 @@ ELF_HANDLE_DECL(elf_sym) elf_sym_by_name(struct elf_binary *elf, const char *sym
635
return ELF_INVALID_HANDLE(elf_sym);
638
-ELF_HANDLE_DECL(elf_sym) elf_sym_by_index(struct elf_binary *elf, int index)
639
+ELF_HANDLE_DECL(elf_sym) elf_sym_by_index(struct elf_binary *elf, unsigned index)
641
ELF_PTRVAL_CONST_VOID ptr = elf_section_start(elf, elf->sym_tab);
642
ELF_HANDLE_DECL(elf_sym) sym;
643
@@ -280,7 +280,7 @@ const char *elf_note_name(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) note
645
ELF_PTRVAL_CONST_VOID elf_note_desc(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) note)
647
- int namesz = (elf_uval(elf, note, namesz) + 3) & ~3;
648
+ unsigned namesz = (elf_uval(elf, note, namesz) + 3) & ~3;
650
return ELF_HANDLE_PTRVAL(note) + elf_size(elf, note) + namesz;
652
@@ -288,7 +288,7 @@ ELF_PTRVAL_CONST_VOID elf_note_desc(struct elf_binary *elf, ELF_HANDLE_DECL(elf_
653
uint64_t elf_note_numeric(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) note)
655
ELF_PTRVAL_CONST_VOID desc = elf_note_desc(elf, note);
656
- int descsz = elf_uval(elf, note, descsz);
657
+ unsigned descsz = elf_uval(elf, note, descsz);
661
@@ -306,7 +306,7 @@ uint64_t elf_note_numeric_array(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note
662
unsigned int unitsz, unsigned int idx)
664
ELF_PTRVAL_CONST_VOID desc = elf_note_desc(elf, note);
665
- int descsz = elf_uval(elf, note, descsz);
666
+ unsigned descsz = elf_uval(elf, note, descsz);
668
if ( descsz % unitsz || idx >= descsz / unitsz )
670
@@ -324,8 +324,8 @@ uint64_t elf_note_numeric_array(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note
672
ELF_HANDLE_DECL(elf_note) elf_note_next(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) note)
674
- int namesz = (elf_uval(elf, note, namesz) + 3) & ~3;
675
- int descsz = (elf_uval(elf, note, descsz) + 3) & ~3;
676
+ unsigned namesz = (elf_uval(elf, note, namesz) + 3) & ~3;
677
+ unsigned descsz = (elf_uval(elf, note, descsz) + 3) & ~3;
679
return ELF_MAKE_HANDLE(elf_note, ELF_HANDLE_PTRVAL(note) + elf_size(elf, note) + namesz + descsz);
681
diff --git a/xen/include/xen/libelf.h b/xen/include/xen/libelf.h
682
index 951430f..87e126a 100644
683
--- a/xen/include/xen/libelf.h
684
+++ b/xen/include/xen/libelf.h
689
+typedef int elf_errorstatus; /* 0: ok; -ve (normally -1): error */
690
+typedef int elf_negerrnoval; /* 0: ok; -EFOO: error */
693
#include "elfstructs.h"
695
@@ -328,12 +331,12 @@ bool elf_access_ok(struct elf_binary * elf,
696
/* ------------------------------------------------------------------------ */
697
/* xc_libelf_tools.c */
699
-int elf_shdr_count(struct elf_binary *elf);
700
-int elf_phdr_count(struct elf_binary *elf);
701
+unsigned elf_shdr_count(struct elf_binary *elf);
702
+unsigned elf_phdr_count(struct elf_binary *elf);
704
ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_name(struct elf_binary *elf, const char *name);
705
-ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_index(struct elf_binary *elf, int index);
706
-ELF_HANDLE_DECL(elf_phdr) elf_phdr_by_index(struct elf_binary *elf, int index);
707
+ELF_HANDLE_DECL(elf_shdr) elf_shdr_by_index(struct elf_binary *elf, unsigned index);
708
+ELF_HANDLE_DECL(elf_phdr) elf_phdr_by_index(struct elf_binary *elf, unsigned index);
710
const char *elf_section_name(struct elf_binary *elf, ELF_HANDLE_DECL(elf_shdr) shdr); /* might return NULL if inputs are invalid */
711
ELF_PTRVAL_CONST_VOID elf_section_start(struct elf_binary *elf, ELF_HANDLE_DECL(elf_shdr) shdr);
712
@@ -343,7 +346,7 @@ ELF_PTRVAL_CONST_VOID elf_segment_start(struct elf_binary *elf, ELF_HANDLE_DECL(
713
ELF_PTRVAL_CONST_VOID elf_segment_end(struct elf_binary *elf, ELF_HANDLE_DECL(elf_phdr) phdr);
715
ELF_HANDLE_DECL(elf_sym) elf_sym_by_name(struct elf_binary *elf, const char *symbol);
716
-ELF_HANDLE_DECL(elf_sym) elf_sym_by_index(struct elf_binary *elf, int index);
717
+ELF_HANDLE_DECL(elf_sym) elf_sym_by_index(struct elf_binary *elf, unsigned index);
719
const char *elf_note_name(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) note); /* may return NULL */
720
ELF_PTRVAL_CONST_VOID elf_note_desc(struct elf_binary *elf, ELF_HANDLE_DECL(elf_note) note);
721
@@ -360,7 +363,7 @@ bool elf_phdr_is_loadable(struct elf_binary *elf, ELF_HANDLE_DECL(elf_phdr) phdr
722
/* ------------------------------------------------------------------------ */
723
/* xc_libelf_loader.c */
725
-int elf_init(struct elf_binary *elf, const char *image, size_t size);
726
+elf_errorstatus elf_init(struct elf_binary *elf, const char *image, size_t size);
728
* image and size must be correct. They will be recorded in
729
* *elf, and must remain valid while the elf is in use.
730
@@ -373,7 +376,7 @@ void elf_set_log(struct elf_binary *elf, elf_log_callback*,
733
void elf_parse_binary(struct elf_binary *elf);
734
-int elf_load_binary(struct elf_binary *elf);
735
+elf_errorstatus elf_load_binary(struct elf_binary *elf);
737
ELF_PTRVAL_VOID elf_get_ptr(struct elf_binary *elf, unsigned long addr);
738
uint64_t elf_lookup_addr(struct elf_binary *elf, const char *symbol);
739
@@ -386,7 +389,7 @@ const char *elf_check_broken(const struct elf_binary *elf); /* NULL means OK */
740
/* ------------------------------------------------------------------------ */
741
/* xc_libelf_relocate.c */
743
-int elf_reloc(struct elf_binary *elf);
744
+elf_errorstatus elf_reloc(struct elf_binary *elf);
746
/* ------------------------------------------------------------------------ */
747
/* xc_libelf_dominfo.c */
748
@@ -420,7 +423,7 @@ struct elf_dom_parms {
753
+ int pae; /* some kind of enum apparently */