# HG changeset patch
# User kfraser@xxxxxxxxxxxxxxxxxxxxx
# Date 1171469170 0
# Node ID 58e71ae679d5d2e602886297c134f12148540f47
# Parent 3f63c1825b0dd86281657aa1b9715fc93a88a2ac
libelf: use xen code style.
Signed-off-by: Keir Fraser <keir@xxxxxxxxxxxxx>
---
xen/common/libelf/libelf-dominfo.c | 557 ++++++++++++++++++------------------
xen/common/libelf/libelf-loader.c | 123 ++++---
xen/common/libelf/libelf-private.h | 26 +
xen/common/libelf/libelf-relocate.c | 375 ++++++++++++------------
xen/common/libelf/libelf-tools.c | 153 +++++----
5 files changed, 650 insertions(+), 584 deletions(-)
diff -r 3f63c1825b0d -r 58e71ae679d5 xen/common/libelf/libelf-dominfo.c
--- a/xen/common/libelf/libelf-dominfo.c Wed Feb 14 16:05:28 2007 +0000
+++ b/xen/common/libelf/libelf-dominfo.c Wed Feb 14 16:06:10 2007 +0000
@@ -15,61 +15,63 @@ const char *elf_xen_feature_names[] = {
[XENFEAT_pae_pgdir_above_4gb] = "pae_pgdir_above_4gb"
};
const int elf_xen_features =
- sizeof(elf_xen_feature_names) / sizeof(elf_xen_feature_names[0]);
+sizeof(elf_xen_feature_names) / sizeof(elf_xen_feature_names[0]);
int elf_xen_parse_features(const char *features,
- uint32_t *supported,
- uint32_t *required)
+ uint32_t *supported,
+ uint32_t *required)
{
char feature[64];
int pos, len, i;
- if (NULL == features)
- return 0;
- for (pos = 0; features[pos] != '\0'; pos += len)
- {
- memset(feature, 0, sizeof(feature));
- for (len = 0;; len++)
- {
- if (len >= sizeof(feature)-1)
- break;
- if (features[pos + len] == '\0')
- break;
- if (features[pos + len] == '|')
- {
- len++;
- break;
- }
- feature[len] = features[pos + len];
- }
-
- for (i = 0; i < elf_xen_features; i++)
- {
- if (!elf_xen_feature_names[i])
- continue;
- if (NULL != required && feature[0] == '!')
- {
- /* required */
- if (0 == strcmp(feature + 1, elf_xen_feature_names[i]))
- {
- elf_xen_feature_set(i, supported);
- elf_xen_feature_set(i, required);
- break;
- }
- }
- else
- {
- /* supported */
- if (0 == strcmp(feature, elf_xen_feature_names[i]))
- {
- elf_xen_feature_set(i, supported);
- break;
- }
- }
- }
- if (i == elf_xen_features)
- return -1;
- }
+ if ( features == NULL )
+ return 0;
+
+ for ( pos = 0; features[pos] != '\0'; pos += len )
+ {
+ memset(feature, 0, sizeof(feature));
+ for ( len = 0;; len++ )
+ {
+ if ( len >= sizeof(feature)-1 )
+ break;
+ if ( features[pos + len] == '\0' )
+ break;
+ if ( features[pos + len] == '|' )
+ {
+ len++;
+ break;
+ }
+ feature[len] = features[pos + len];
+ }
+
+ for ( i = 0; i < elf_xen_features; i++ )
+ {
+ if ( !elf_xen_feature_names[i] )
+ continue;
+ if ( (required != NULL) && (feature[0] == '!') )
+ {
+ /* required */
+ if ( !strcmp(feature + 1, elf_xen_feature_names[i]) )
+ {
+ elf_xen_feature_set(i, supported);
+ elf_xen_feature_set(i, required);
+ break;
+ }
+ }
+ else
+ {
+ /* supported */
+ if ( !strcmp(feature, elf_xen_feature_names[i]) )
+ {
+ elf_xen_feature_set(i, supported);
+ break;
+ }
+ }
+ }
+ if ( i == elf_xen_features )
+ return -1;
+ }
+
return 0;
}
@@ -77,26 +79,26 @@ int elf_xen_parse_features(const char *f
/* xen elf notes */
int elf_xen_parse_note(struct elf_binary *elf,
- struct elf_dom_parms *parms,
- const elf_note *note)
+ struct elf_dom_parms *parms,
+ const elf_note *note)
{
/* *INDENT-OFF* */
static const struct {
- char *name;
- int str;
+ char *name;
+ int str;
} note_desc[] = {
- [XEN_ELFNOTE_ENTRY] = { "ENTRY", 0},
- [XEN_ELFNOTE_HYPERCALL_PAGE] = { "HYPERCALL_PAGE", 0},
- [XEN_ELFNOTE_VIRT_BASE] = { "VIRT_BASE", 0},
- [XEN_ELFNOTE_PADDR_OFFSET] = { "PADDR_OFFSET", 0},
- [XEN_ELFNOTE_HV_START_LOW] = { "HV_START_LOW", 0},
- [XEN_ELFNOTE_XEN_VERSION] = { "XEN_VERSION", 1},
- [XEN_ELFNOTE_GUEST_OS] = { "GUEST_OS", 1},
- [XEN_ELFNOTE_GUEST_VERSION] = { "GUEST_VERSION", 1},
- [XEN_ELFNOTE_LOADER] = { "LOADER", 1},
- [XEN_ELFNOTE_PAE_MODE] = { "PAE_MODE", 1},
- [XEN_ELFNOTE_FEATURES] = { "FEATURES", 1},
- [XEN_ELFNOTE_BSD_SYMTAB] = { "BSD_SYMTAB", 1},
+ [XEN_ELFNOTE_ENTRY] = { "ENTRY", 0},
+ [XEN_ELFNOTE_HYPERCALL_PAGE] = { "HYPERCALL_PAGE", 0},
+ [XEN_ELFNOTE_VIRT_BASE] = { "VIRT_BASE", 0},
+ [XEN_ELFNOTE_PADDR_OFFSET] = { "PADDR_OFFSET", 0},
+ [XEN_ELFNOTE_HV_START_LOW] = { "HV_START_LOW", 0},
+ [XEN_ELFNOTE_XEN_VERSION] = { "XEN_VERSION", 1},
+ [XEN_ELFNOTE_GUEST_OS] = { "GUEST_OS", 1},
+ [XEN_ELFNOTE_GUEST_VERSION] = { "GUEST_VERSION", 1},
+ [XEN_ELFNOTE_LOADER] = { "LOADER", 1},
+ [XEN_ELFNOTE_PAE_MODE] = { "PAE_MODE", 1},
+ [XEN_ELFNOTE_FEATURES] = { "FEATURES", 1},
+ [XEN_ELFNOTE_BSD_SYMTAB] = { "BSD_SYMTAB", 1},
};
/* *INDENT-ON* */
@@ -104,73 +106,73 @@ int elf_xen_parse_note(struct elf_binary
uint64_t val = 0;
int type = elf_uval(elf, note, type);
- if ((type >= sizeof(note_desc) / sizeof(note_desc[0])) ||
- (NULL == note_desc[type].name))
- {
- elf_msg(elf, "%s: unknown xen elf note (0x%x)\n",
- __FUNCTION__, type);
- return 0;
- }
-
- if (note_desc[type].str)
- {
- str = elf_note_desc(elf, note);
- elf_msg(elf, "%s: %s = \"%s\"\n", __FUNCTION__,
- note_desc[type].name, str);
+ if ( (type >= sizeof(note_desc) / sizeof(note_desc[0])) ||
+ (note_desc[type].name == NULL) )
+ {
+ elf_msg(elf, "%s: unknown xen elf note (0x%x)\n",
+ __FUNCTION__, type);
+ return 0;
+ }
+
+ if ( note_desc[type].str )
+ {
+ str = elf_note_desc(elf, note);
+ elf_msg(elf, "%s: %s = \"%s\"\n", __FUNCTION__,
+ note_desc[type].name, str);
}
else
{
- val = elf_note_numeric(elf, note);
- elf_msg(elf, "%s: %s = 0x%" PRIx64 "\n", __FUNCTION__,
- note_desc[type].name, val);
- }
-
- switch (type)
+ val = elf_note_numeric(elf, note);
+ elf_msg(elf, "%s: %s = 0x%" PRIx64 "\n", __FUNCTION__,
+ note_desc[type].name, val);
+ }
+
+ switch ( type )
{
case XEN_ELFNOTE_LOADER:
- safe_strcpy(parms->loader, str);
- break;
+ safe_strcpy(parms->loader, str);
+ break;
case XEN_ELFNOTE_GUEST_OS:
- safe_strcpy(parms->guest_os, str);
- break;
+ safe_strcpy(parms->guest_os, str);
+ break;
case XEN_ELFNOTE_GUEST_VERSION:
- safe_strcpy(parms->guest_ver, str);
- break;
+ safe_strcpy(parms->guest_ver, str);
+ break;
case XEN_ELFNOTE_XEN_VERSION:
- safe_strcpy(parms->xen_ver, str);
- break;
+ safe_strcpy(parms->xen_ver, str);
+ break;
case XEN_ELFNOTE_PAE_MODE:
- if (0 == strcmp(str, "yes"))
- parms->pae = 2 /* extended_cr3 */;
- if (strstr(str, "bimodal"))
- parms->pae = 3 /* bimodal */;
- break;
+ if ( !strcmp(str, "yes") )
+ parms->pae = 2 /* extended_cr3 */;
+ if ( strstr(str, "bimodal") )
+ parms->pae = 3 /* bimodal */;
+ break;
case XEN_ELFNOTE_BSD_SYMTAB:
- if (0 == strcmp(str, "yes"))
- parms->bsd_symtab = 1;
- break;
+ if ( !strcmp(str, "yes") )
+ parms->bsd_symtab = 1;
+ break;
case XEN_ELFNOTE_VIRT_BASE:
- parms->virt_base = val;
- break;
+ parms->virt_base = val;
+ break;
case XEN_ELFNOTE_ENTRY:
- parms->virt_entry = val;
- break;
+ parms->virt_entry = val;
+ break;
case XEN_ELFNOTE_PADDR_OFFSET:
- parms->elf_paddr_offset = val;
- break;
+ parms->elf_paddr_offset = val;
+ break;
case XEN_ELFNOTE_HYPERCALL_PAGE:
- parms->virt_hypercall = val;
- break;
+ parms->virt_hypercall = val;
+ break;
case XEN_ELFNOTE_HV_START_LOW:
- parms->virt_hv_start_low = val;
- break;
+ parms->virt_hv_start_low = val;
+ break;
case XEN_ELFNOTE_FEATURES:
- if (0 != elf_xen_parse_features(str, parms->f_supported,
- parms->f_required))
- return -1;
- break;
+ if ( elf_xen_parse_features(str, parms->f_supported,
+ parms->f_required) )
+ return -1;
+ break;
}
return 0;
@@ -180,83 +182,85 @@ int elf_xen_parse_note(struct elf_binary
/* __xen_guest section */
int elf_xen_parse_guest_info(struct elf_binary *elf,
- struct elf_dom_parms *parms)
+ struct elf_dom_parms *parms)
{
const char *h;
char name[32], value[128];
int len;
h = parms->guest_info;
- while (*h)
- {
- memset(name, 0, sizeof(name));
- memset(value, 0, sizeof(value));
- for (len = 0;; len++, h++) {
- if (len >= sizeof(name)-1)
- break;
- if (*h == '\0')
- break;
- if (*h == ',')
- {
- h++;
- break;
- }
- if (*h == '=')
- {
- h++;
- for (len = 0;; len++, h++) {
- if (len >= sizeof(value)-1)
- break;
- if (*h == '\0')
- break;
- if (*h == ',')
- {
- h++;
- break;
- }
- value[len] = *h;
- }
- break;
- }
- name[len] = *h;
- }
- elf_msg(elf, "%s: %s=\"%s\"\n", __FUNCTION__, name, value);
-
- /* strings */
- if (0 == strcmp(name, "LOADER"))
- safe_strcpy(parms->loader, value);
- if (0 == strcmp(name, "GUEST_OS"))
- safe_strcpy(parms->guest_os, value);
- if (0 == strcmp(name, "GUEST_VER"))
- safe_strcpy(parms->guest_ver, value);
- if (0 == strcmp(name, "XEN_VER"))
- safe_strcpy(parms->xen_ver, value);
- if (0 == strcmp(name, "PAE"))
- {
- if (0 == strcmp(value, "yes[extended-cr3]"))
- parms->pae = 2 /* extended_cr3 */;
- else if (0 == strncmp(value, "yes", 3))
- parms->pae = 1 /* yes */;
- }
- if (0 == strcmp(name, "BSD_SYMTAB"))
- parms->bsd_symtab = 1;
-
- /* longs */
- if (0 == strcmp(name, "VIRT_BASE"))
- parms->virt_base = strtoull(value, NULL, 0);
- if (0 == strcmp(name, "VIRT_ENTRY"))
- parms->virt_entry = strtoull(value, NULL, 0);
- if (0 == strcmp(name, "ELF_PADDR_OFFSET"))
- parms->elf_paddr_offset = strtoull(value, NULL, 0);
- if (0 == strcmp(name, "HYPERCALL_PAGE"))
- parms->virt_hypercall = (strtoull(value, NULL, 0) << 12) +
- parms->virt_base;
-
- /* other */
- if (0 == strcmp(name, "FEATURES"))
- if (0 != elf_xen_parse_features(value, parms->f_supported,
- parms->f_required))
- return -1;
+ while ( *h )
+ {
+ memset(name, 0, sizeof(name));
+ memset(value, 0, sizeof(value));
+ for ( len = 0;; len++, h++ )
+ {
+ if ( len >= sizeof(name)-1 )
+ break;
+ if ( *h == '\0' )
+ break;
+ if ( *h == ',' )
+ {
+ h++;
+ break;
+ }
+ if ( *h == '=' )
+ {
+ h++;
+ for ( len = 0;; len++, h++ )
+ {
+ if ( len >= sizeof(value)-1 )
+ break;
+ if ( *h == '\0' )
+ break;
+ if ( *h == ',' )
+ {
+ h++;
+ break;
+ }
+ value[len] = *h;
+ }
+ break;
+ }
+ name[len] = *h;
+ }
+ elf_msg(elf, "%s: %s=\"%s\"\n", __FUNCTION__, name, value);
+
+ /* strings */
+ if ( !strcmp(name, "LOADER") )
+ safe_strcpy(parms->loader, value);
+ if ( !strcmp(name, "GUEST_OS") )
+ safe_strcpy(parms->guest_os, value);
+ if ( !strcmp(name, "GUEST_VER") )
+ safe_strcpy(parms->guest_ver, value);
+ if ( !strcmp(name, "XEN_VER") )
+ safe_strcpy(parms->xen_ver, value);
+ if ( !strcmp(name, "PAE") )
+ {
+ if ( !strcmp(value, "yes[extended-cr3]") )
+ parms->pae = 2 /* extended_cr3 */;
+ else if ( !strncmp(value, "yes", 3) )
+ parms->pae = 1 /* yes */;
+ }
+ if ( !strcmp(name, "BSD_SYMTAB") )
+ parms->bsd_symtab = 1;
+
+ /* longs */
+ if ( !strcmp(name, "VIRT_BASE") )
+ parms->virt_base = strtoull(value, NULL, 0);
+ if ( !strcmp(name, "VIRT_ENTRY") )
+ parms->virt_entry = strtoull(value, NULL, 0);
+ if ( !strcmp(name, "ELF_PADDR_OFFSET") )
+ parms->elf_paddr_offset = strtoull(value, NULL, 0);
+ if ( !strcmp(name, "HYPERCALL_PAGE") )
+ parms->virt_hypercall = (strtoull(value, NULL, 0) << 12) +
+ parms->virt_base;
+
+ /* other */
+ if ( !strcmp(name, "FEATURES") )
+ if ( elf_xen_parse_features(value, parms->f_supported,
+ parms->f_required) )
+ return -1;
}
return 0;
}
@@ -265,54 +269,59 @@ int elf_xen_parse_guest_info(struct elf_
/* sanity checks */
static int elf_xen_note_check(struct elf_binary *elf,
- struct elf_dom_parms *parms)
-{
- if (NULL == parms->elf_note_start && NULL == parms->guest_info) {
- int machine = elf_uval(elf, elf->ehdr, e_machine);
- if (EM_386 == machine || EM_X86_64 == machine) {
- elf_err(elf, "%s: ERROR: Not a Xen-ELF image: "
- "No ELF notes or '__xen_guest' section found.\n",
- __FUNCTION__);
- return -1;
- }
- return 0;
+ struct elf_dom_parms *parms)
+{
+ if ( (parms->elf_note_start == NULL) && (parms->guest_info == NULL) )
+ {
+ int machine = elf_uval(elf, elf->ehdr, e_machine);
+ if ( (machine == EM_386) || (machine == EM_X86_64) )
+ {
+ elf_err(elf, "%s: ERROR: Not a Xen-ELF image: "
+ "No ELF notes or '__xen_guest' section found.\n",
+ __FUNCTION__);
+ return -1;
+ }
+ return 0;
}
/* Check the contents of the Xen notes or guest string. */
- if ( ( 0 == strlen(parms->loader) || strncmp(parms->loader, "generic", 7)
) &&
- ( 0 == strlen(parms->guest_os) || strncmp(parms->guest_os, "linux", 5)
) )
- {
- elf_err(elf, "%s: ERROR: Will only load images built for the generic "
- "loader or Linux images", __FUNCTION__);
- return -1;
- }
-
- if ( 0 == strlen(parms->xen_ver) || strncmp(parms->xen_ver, "xen-3.0", 7) )
- {
- elf_err(elf, "%s: ERROR: Xen will only load images built for Xen
v3.0\n",
- __FUNCTION__);
- return -1;
+ if ( ((strlen(parms->loader) == 0) ||
+ strncmp(parms->loader, "generic", 7)) &&
+ ((strlen(parms->guest_os) == 0) ||
+ strncmp(parms->guest_os, "linux", 5)) )
+ {
+ elf_err(elf, "%s: ERROR: Will only load images built for the generic "
+ "loader or Linux images", __FUNCTION__);
+ return -1;
+ }
+
+ if ( (strlen(parms->xen_ver) == 0) ||
+ strncmp(parms->xen_ver, "xen-3.0", 7) )
+ {
+ elf_err(elf, "%s: ERROR: Xen will only load images built "
+ "for Xen v3.0\n", __FUNCTION__);
+ return -1;
}
return 0;
}
static int elf_xen_addr_calc_check(struct elf_binary *elf,
- struct elf_dom_parms *parms)
-{
- if (UNSET_ADDR != parms->elf_paddr_offset &&
- UNSET_ADDR == parms->virt_base )
- {
- elf_err(elf, "%s: ERROR: ELF_PADDR_OFFSET set, VIRT_BASE unset\n",
- __FUNCTION__);
+ struct elf_dom_parms *parms)
+{
+ if ( (parms->elf_paddr_offset != UNSET_ADDR) &&
+ (parms->virt_base == UNSET_ADDR) )
+ {
+ elf_err(elf, "%s: ERROR: ELF_PADDR_OFFSET set, VIRT_BASE unset\n",
+ __FUNCTION__);
return -1;
}
/* Initial guess for virt_base is 0 if it is not explicitly defined. */
- if (UNSET_ADDR == parms->virt_base)
- {
- parms->virt_base = 0;
- elf_msg(elf, "%s: VIRT_BASE unset, using 0x%" PRIx64 "\n",
- __FUNCTION__, parms->virt_base);
+ if ( parms->virt_base == UNSET_ADDR )
+ {
+ parms->virt_base = 0;
+ elf_msg(elf, "%s: VIRT_BASE unset, using 0x%" PRIx64 "\n",
+ __FUNCTION__, parms->virt_base);
}
/*
@@ -324,22 +333,22 @@ static int elf_xen_addr_calc_check(struc
* If we are using the modern ELF notes interface then the default
* is 0.
*/
- if (UNSET_ADDR == parms->elf_paddr_offset)
- {
- if (parms->elf_note_start)
- parms->elf_paddr_offset = 0;
- else
- parms->elf_paddr_offset = parms->virt_base;
- elf_msg(elf, "%s: ELF_PADDR_OFFSET unset, using 0x%" PRIx64 "\n",
- __FUNCTION__, parms->elf_paddr_offset);
+ if ( parms->elf_paddr_offset == UNSET_ADDR )
+ {
+ if ( parms->elf_note_start )
+ parms->elf_paddr_offset = 0;
+ else
+ parms->elf_paddr_offset = parms->virt_base;
+ elf_msg(elf, "%s: ELF_PADDR_OFFSET unset, using 0x%" PRIx64 "\n",
+ __FUNCTION__, parms->elf_paddr_offset);
}
parms->virt_offset = parms->virt_base - parms->elf_paddr_offset;
parms->virt_kstart = elf->pstart + parms->virt_offset;
parms->virt_kend = elf->pend + parms->virt_offset;
- if (UNSET_ADDR == parms->virt_entry)
- parms->virt_entry = elf_uval(elf, elf->ehdr, e_entry);
+ if ( parms->virt_entry == UNSET_ADDR )
+ parms->virt_entry = elf_uval(elf, elf->ehdr, e_entry);
elf_msg(elf, "%s: addresses:\n", __FUNCTION__);
elf_msg(elf, " virt_base = 0x%" PRIx64 "\n", parms->virt_base);
@@ -355,7 +364,7 @@ static int elf_xen_addr_calc_check(struc
(parms->virt_base > parms->virt_kstart) )
{
elf_err(elf, "%s: ERROR: ELF start or entries are out of bounds.\n",
- __FUNCTION__);
+ __FUNCTION__);
return -1;
}
@@ -366,7 +375,7 @@ static int elf_xen_addr_calc_check(struc
/* glue it all together ... */
int elf_xen_parse(struct elf_binary *elf,
- struct elf_dom_parms *parms)
+ struct elf_dom_parms *parms)
{
const elf_note *note;
const elf_shdr *shdr;
@@ -382,39 +391,49 @@ int elf_xen_parse(struct elf_binary *elf
/* find and parse elf notes */
count = elf_shdr_count(elf);
- for (i = 0; i < count; i++)
- {
- shdr = elf_shdr_by_index(elf, i);
- if (0 == strcmp(elf_section_name(elf, shdr), "__xen_guest"))
- parms->guest_info = elf_section_start(elf, shdr);
- if (elf_uval(elf, shdr, sh_type) != SHT_NOTE)
- continue;
- parms->elf_note_start = elf_section_start(elf, shdr);
- parms->elf_note_end = elf_section_end(elf, shdr);
- for (note = parms->elf_note_start;
- (void *)note < parms->elf_note_end;
- note = elf_note_next(elf, note))
- {
- if (0 != strcmp(elf_note_name(elf, note), "Xen"))
- continue;
- if (0 != elf_xen_parse_note(elf, parms, note))
- return -1;
- xen_elfnotes++;
- }
- }
-
- if (!xen_elfnotes && parms->guest_info)
- {
- parms->elf_note_start = NULL;
- parms->elf_note_end = NULL;
- elf_msg(elf, "%s: __xen_guest: \"%s\"\n", __FUNCTION__,
- parms->guest_info);
- elf_xen_parse_guest_info(elf, parms);
- }
-
- if (0 != elf_xen_note_check(elf, parms))
- return -1;
- if (0 != elf_xen_addr_calc_check(elf, parms))
- return -1;
- return 0;
-}
+ for ( i = 0; i < count; i++ )
+ {
+ shdr = elf_shdr_by_index(elf, i);
+ if ( !strcmp(elf_section_name(elf, shdr), "__xen_guest") )
+ parms->guest_info = elf_section_start(elf, shdr);
+ if ( elf_uval(elf, shdr, sh_type) != SHT_NOTE )
+ continue;
+ parms->elf_note_start = elf_section_start(elf, shdr);
+ parms->elf_note_end = elf_section_end(elf, shdr);
+ for ( note = parms->elf_note_start;
+ (void *)note < parms->elf_note_end;
+ note = elf_note_next(elf, note) )
+ {
+ if ( strcmp(elf_note_name(elf, note), "Xen") )
+ continue;
+ if ( elf_xen_parse_note(elf, parms, note) )
+ return -1;
+ xen_elfnotes++;
+ }
+ }
+
+ if ( !xen_elfnotes && parms->guest_info )
+ {
+ parms->elf_note_start = NULL;
+ parms->elf_note_end = NULL;
+ elf_msg(elf, "%s: __xen_guest: \"%s\"\n", __FUNCTION__,
+ parms->guest_info);
+ elf_xen_parse_guest_info(elf, parms);
+ }
+
+ if ( elf_xen_note_check(elf, parms) != 0 )
+ return -1;
+ if ( elf_xen_addr_calc_check(elf, parms) != 0 )
+ return -1;
+ return 0;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-set-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff -r 3f63c1825b0d -r 58e71ae679d5 xen/common/libelf/libelf-loader.c
--- a/xen/common/libelf/libelf-loader.c Wed Feb 14 16:05:28 2007 +0000
+++ b/xen/common/libelf/libelf-loader.c Wed Feb 14 16:06:10 2007 +0000
@@ -11,10 +11,10 @@ int elf_init(struct elf_binary *elf, con
const elf_shdr *shdr;
uint64_t i, count, section, offset;
- if (!elf_is_elfbinary(image))
+ if ( !elf_is_elfbinary(image) )
{
- elf_err(elf, "%s: not an ELF binary\n", __FUNCTION__);
- return -1;
+ elf_err(elf, "%s: not an ELF binary\n", __FUNCTION__);
+ return -1;
}
memset(elf, 0, sizeof(*elf));
@@ -26,46 +26,46 @@ int elf_init(struct elf_binary *elf, con
/* sanity check phdr */
offset = elf_uval(elf, elf->ehdr, e_phoff) +
- elf_uval(elf, elf->ehdr, e_phentsize) * elf_phdr_count(elf);
- if (offset > elf->size)
+ elf_uval(elf, elf->ehdr, e_phentsize) * elf_phdr_count(elf);
+ if ( offset > elf->size )
{
- elf_err(elf, "%s: phdr overflow (off %" PRIx64 " > size %lx)\n",
- __FUNCTION__, offset, (unsigned long)elf->size);
- return -1;
+ elf_err(elf, "%s: phdr overflow (off %" PRIx64 " > size %lx)\n",
+ __FUNCTION__, offset, (unsigned long)elf->size);
+ return -1;
}
/* sanity check shdr */
offset = elf_uval(elf, elf->ehdr, e_shoff) +
- elf_uval(elf, elf->ehdr, e_shentsize) * elf_shdr_count(elf);
- if (offset > elf->size)
+ elf_uval(elf, elf->ehdr, e_shentsize) * elf_shdr_count(elf);
+ if ( offset > elf->size )
{
- elf_err(elf, "%s: shdr overflow (off %" PRIx64 " > size %lx)\n",
- __FUNCTION__, offset, (unsigned long)elf->size);
- return -1;
+ elf_err(elf, "%s: shdr overflow (off %" PRIx64 " > size %lx)\n",
+ __FUNCTION__, offset, (unsigned long)elf->size);
+ return -1;
}
/* find section string table */
section = elf_uval(elf, elf->ehdr, e_shstrndx);
shdr = elf_shdr_by_index(elf, section);
- if (NULL != shdr)
- elf->sec_strtab = elf_section_start(elf, shdr);
+ if ( shdr != NULL )
+ elf->sec_strtab = elf_section_start(elf, shdr);
/* find symbol table, symbol string table */
count = elf_shdr_count(elf);
- for (i = 0; i < count; i++)
+ for ( i = 0; i < count; i++ )
{
- shdr = elf_shdr_by_index(elf, i);
- if (elf_uval(elf, shdr, sh_type) != SHT_SYMTAB)
- continue;
- elf->sym_tab = shdr;
- shdr = elf_shdr_by_index(elf, elf_uval(elf, shdr, sh_link));
- if (NULL == shdr)
- {
- elf->sym_tab = NULL;
- continue;
- }
- elf->sym_strtab = elf_section_start(elf, shdr);
- break;
+ shdr = elf_shdr_by_index(elf, i);
+ if ( elf_uval(elf, shdr, sh_type) != SHT_SYMTAB )
+ continue;
+ elf->sym_tab = shdr;
+ shdr = elf_shdr_by_index(elf, elf_uval(elf, shdr, sh_link));
+ if ( shdr == NULL )
+ {
+ elf->sym_tab = NULL;
+ continue;
+ }
+ elf->sym_strtab = elf_section_start(elf, shdr);
+ break;
}
return 0;
}
@@ -91,24 +91,24 @@ void elf_parse_binary(struct elf_binary
uint64_t i, count, paddr, memsz;
count = elf_uval(elf, elf->ehdr, e_phnum);
- for (i = 0; i < count; i++)
+ for ( i = 0; i < count; i++ )
{
- phdr = elf_phdr_by_index(elf, i);
- if (!elf_phdr_is_loadable(elf, phdr))
- continue;
- paddr = elf_uval(elf, phdr, p_paddr);
- memsz = elf_uval(elf, phdr, p_memsz);
- elf_msg(elf, "%s: phdr: paddr=0x%" PRIx64
- " memsz=0x%" PRIx64 "\n", __FUNCTION__, paddr, memsz);
- if (low > paddr)
- low = paddr;
- if (high < paddr + memsz)
- high = paddr + memsz;
+ phdr = elf_phdr_by_index(elf, i);
+ if ( !elf_phdr_is_loadable(elf, phdr) )
+ continue;
+ paddr = elf_uval(elf, phdr, p_paddr);
+ memsz = elf_uval(elf, phdr, p_memsz);
+ elf_msg(elf, "%s: phdr: paddr=0x%" PRIx64
+ " memsz=0x%" PRIx64 "\n", __FUNCTION__, paddr, memsz);
+ if ( low > paddr )
+ low = paddr;
+ if ( high < paddr + memsz )
+ high = paddr + memsz;
}
elf->pstart = low;
elf->pend = high;
elf_msg(elf, "%s: memory: 0x%" PRIx64 " -> 0x%" PRIx64 "\n",
- __FUNCTION__, elf->pstart, elf->pend);
+ __FUNCTION__, elf->pstart, elf->pend);
}
void elf_load_binary(struct elf_binary *elf)
@@ -118,18 +118,18 @@ void elf_load_binary(struct elf_binary *
char *dest;
count = elf_uval(elf, elf->ehdr, e_phnum);
- for (i = 0; i < count; i++)
+ for ( i = 0; i < count; i++ )
{
- phdr = elf_phdr_by_index(elf, i);
- if (!elf_phdr_is_loadable(elf, phdr))
- continue;
- paddr = elf_uval(elf, phdr, p_paddr);
- offset = elf_uval(elf, phdr, p_offset);
- filesz = elf_uval(elf, phdr, p_filesz);
- memsz = elf_uval(elf, phdr, p_memsz);
- dest = elf_get_ptr(elf, paddr);
- memcpy(dest, elf->image + offset, filesz);
- memset(dest + filesz, 0, memsz - filesz);
+ phdr = elf_phdr_by_index(elf, i);
+ if ( !elf_phdr_is_loadable(elf, phdr) )
+ continue;
+ paddr = elf_uval(elf, phdr, p_paddr);
+ offset = elf_uval(elf, phdr, p_offset);
+ filesz = elf_uval(elf, phdr, p_filesz);
+ memsz = elf_uval(elf, phdr, p_memsz);
+ dest = elf_get_ptr(elf, paddr);
+ memcpy(dest, elf->image + offset, filesz);
+ memset(dest + filesz, 0, memsz - filesz);
}
}
@@ -144,13 +144,24 @@ uint64_t elf_lookup_addr(struct elf_bina
uint64_t value;
sym = elf_sym_by_name(elf, symbol);
- if (NULL == sym)
+ if ( sym == NULL )
{
- elf_err(elf, "%s: not found: %s\n", __FUNCTION__, symbol);
- return -1;
+ elf_err(elf, "%s: not found: %s\n", __FUNCTION__, symbol);
+ return -1;
}
+
value = elf_uval(elf, sym, st_value);
elf_msg(elf, "%s: symbol \"%s\" at 0x%" PRIx64 "\n", __FUNCTION__,
- symbol, value);
+ symbol, value);
return value;
}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-set-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff -r 3f63c1825b0d -r 58e71ae679d5 xen/common/libelf/libelf-private.h
--- a/xen/common/libelf/libelf-private.h Wed Feb 14 16:05:28 2007 +0000
+++ b/xen/common/libelf/libelf-private.h Wed Feb 14 16:06:10 2007 +0000
@@ -12,9 +12,9 @@
#include <public/libelf.h>
#define elf_msg(elf, fmt, args ... ) \
- if (elf->verbose) printk(fmt, ## args )
+ if (elf->verbose) printk(fmt, ## args )
#define elf_err(elf, fmt, args ... ) \
- printk(fmt, ## args )
+ printk(fmt, ## args )
#define strtoull(str, end, base) simple_strtoull(str, end, base)
#define bswap_16(x) swab16(x)
@@ -43,12 +43,12 @@
#include "xc_private.h"
#define elf_msg(elf, fmt, args ... ) \
- if (elf->log && elf->verbose) fprintf(elf->log, fmt , ## args )
-#define elf_err(elf, fmt, args ... ) do { \
- if (elf->log) \
- fprintf(elf->log, fmt , ## args ); \
- xc_set_error(XC_INVALID_KERNEL, fmt , ## args ); \
- } while (0)
+ if (elf->log && elf->verbose) fprintf(elf->log, fmt , ## args )
+#define elf_err(elf, fmt, args ... ) do { \
+ if (elf->log) \
+ fprintf(elf->log, fmt , ## args ); \
+ xc_set_error(XC_INVALID_KERNEL, fmt , ## args ); \
+} while (0)
#define safe_strcpy(d,s) \
do { strncpy((d),(s),sizeof((d))-1); \
@@ -58,3 +58,13 @@ do { strncpy((d),(s),sizeof((d))-1);
#endif
#endif /* __LIBELF_PRIVATE_H_ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-set-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff -r 3f63c1825b0d -r 58e71ae679d5 xen/common/libelf/libelf-relocate.c
--- a/xen/common/libelf/libelf-relocate.c Wed Feb 14 16:05:28 2007 +0000
+++ b/xen/common/libelf/libelf-relocate.c Wed Feb 14 16:06:10 2007 +0000
@@ -46,22 +46,22 @@ static const char *rel_names_i386[] = {
};
static int elf_reloc_i386(struct elf_binary *elf, int type,
- uint64_t addr, uint64_t value)
+ uint64_t addr, uint64_t value)
{
void *ptr = elf_get_ptr(elf, addr);
uint32_t *u32;
- switch (type)
+ switch ( type )
{
case 1 /* R_386_32 */ :
- u32 = ptr;
- *u32 += elf->reloc_offset;
- break;
+ u32 = ptr;
+ *u32 += elf->reloc_offset;
+ break;
case 2 /* R_386_PC32 */ :
- /* nothing */
- break;
+ /* nothing */
+ break;
default:
- return -1;
+ return -1;
}
return 0;
}
@@ -96,54 +96,57 @@ static const char *rel_names_x86_64[] =
};
static int elf_reloc_x86_64(struct elf_binary *elf, int type,
- uint64_t addr, uint64_t value)
+ uint64_t addr, uint64_t value)
{
void *ptr = elf_get_ptr(elf, addr);
uint64_t *u64;
uint32_t *u32;
int32_t *s32;
- switch (type)
+ switch ( type )
{
case 1 /* R_X86_64_64 */ :
- u64 = ptr;
- value += elf->reloc_offset;
- *u64 = value;
- break;
+ u64 = ptr;
+ value += elf->reloc_offset;
+ *u64 = value;
+ break;
case 2 /* R_X86_64_PC32 */ :
- u32 = ptr;
- *u32 = value - addr;
- if (*u32 != (uint32_t) (value - addr))
- {
- elf_err(elf, "R_X86_64_PC32 overflow: 0x%" PRIx32 " != 0x%" PRIx32
"\n",
- *u32, (uint32_t) (value - addr));
- return -1;
- }
- break;
+ u32 = ptr;
+ *u32 = value - addr;
+ if ( *u32 != (uint32_t)(value - addr) )
+ {
+ elf_err(elf, "R_X86_64_PC32 overflow: 0x%" PRIx32
+ " != 0x%" PRIx32 "\n",
+ *u32, (uint32_t) (value - addr));
+ return -1;
+ }
+ break;
case 10 /* R_X86_64_32 */ :
- u32 = ptr;
- value += elf->reloc_offset;
- *u32 = value;
- if (*u32 != value)
- {
- elf_err(elf, "R_X86_64_32 overflow: 0x%" PRIx32 " != 0x%" PRIx64
"\n",
- *u32, value);
- return -1;
- }
- break;
+ u32 = ptr;
+ value += elf->reloc_offset;
+ *u32 = value;
+ if ( *u32 != value )
+ {
+ elf_err(elf, "R_X86_64_32 overflow: 0x%" PRIx32
+ " != 0x%" PRIx64 "\n",
+ *u32, value);
+ return -1;
+ }
+ break;
case 11 /* R_X86_64_32S */ :
- s32 = ptr;
- value += elf->reloc_offset;
- *s32 = value;
- if (*s32 != (int64_t) value)
- {
- elf_err(elf, "R_X86_64_32S overflow: 0x%" PRIx32 " != 0x%" PRIx64
"\n",
- *s32, (int64_t) value);
- return -1;
- }
- break;
+ s32 = ptr;
+ value += elf->reloc_offset;
+ *s32 = value;
+ if ( *s32 != (int64_t) value )
+ {
+ elf_err(elf, "R_X86_64_32S overflow: 0x%" PRIx32
+ " != 0x%" PRIx64 "\n",
+ *s32, (int64_t) value);
+ return -1;
+ }
+ break;
default:
- return -1;
+ return -1;
}
return 0;
}
@@ -154,19 +157,19 @@ static struct relocs {
const char **names;
int count;
int (*func) (struct elf_binary * elf, int type, uint64_t addr,
- uint64_t value);
+ uint64_t value);
} relocs[] =
/* *INDENT-OFF* */
{
[EM_386] = {
- .names = rel_names_i386,
- .count = sizeof(rel_names_i386) / sizeof(rel_names_i386[0]),
- .func = elf_reloc_i386,
+ .names = rel_names_i386,
+ .count = sizeof(rel_names_i386) / sizeof(rel_names_i386[0]),
+ .func = elf_reloc_i386,
},
[EM_X86_64] = {
- .names = rel_names_x86_64,
- .count = sizeof(rel_names_x86_64) / sizeof(rel_names_x86_64[0]),
- .func = elf_reloc_x86_64,
+ .names = rel_names_x86_64,
+ .count = sizeof(rel_names_x86_64) / sizeof(rel_names_x86_64[0]),
+ .func = elf_reloc_x86_64,
}
};
/* *INDENT-ON* */
@@ -175,18 +178,18 @@ static struct relocs {
static const char *rela_name(int machine, int type)
{
- if (machine > sizeof(relocs) / sizeof(relocs[0]))
- return "unknown mach";
- if (!relocs[machine].names)
- return "unknown mach";
- if (type > relocs[machine].count)
- return "unknown rela";
+ if ( machine > sizeof(relocs) / sizeof(relocs[0]) )
+ return "unknown mach";
+ if ( !relocs[machine].names )
+ return "unknown mach";
+ if ( type > relocs[machine].count )
+ return "unknown rela";
return relocs[machine].names[type];
}
static int elf_reloc_section(struct elf_binary *elf,
- const elf_shdr * rels,
- const elf_shdr * sect, const elf_shdr * syms)
+ const elf_shdr * rels,
+ const elf_shdr * sect, const elf_shdr * syms)
{
const void *ptr, *end;
const elf_shdr *shdr;
@@ -204,18 +207,18 @@ static int elf_reloc_section(struct elf_
int machine;
machine = elf_uval(elf, elf->ehdr, e_machine);
- if (machine >= sizeof(relocs) / sizeof(relocs[0]) ||
- NULL == relocs[machine].func)
- {
- elf_err(elf, "%s: can't handle machine %d\n",
- __FUNCTION__, machine);
- return -1;
- }
- if (elf_swap(elf))
- {
- elf_err(elf, "%s: non-native byte order, relocation not supported\n",
- __FUNCTION__);
- return -1;
+ if ( (machine >= (sizeof(relocs) / sizeof(relocs[0]))) ||
+ (relocs[machine].func == NULL) )
+ {
+ elf_err(elf, "%s: can't handle machine %d\n",
+ __FUNCTION__, machine);
+ return -1;
+ }
+ if ( elf_swap(elf) )
+ {
+ elf_err(elf, "%s: non-native byte order, relocation not supported\n",
+ __FUNCTION__);
+ return -1;
}
s_type = elf_uval(elf, rels, sh_type);
@@ -223,89 +226,89 @@ static int elf_reloc_section(struct elf_
ptr = elf_section_start(elf, rels);
end = elf_section_end(elf, rels);
- for (; ptr < end; ptr += rsize)
- {
- switch (s_type)
- {
- case SHT_REL:
- rel = ptr;
- r_offset = elf_uval(elf, rel, r_offset);
- r_info = elf_uval(elf, rel, r_info);
- r_addend = 0;
- break;
- case SHT_RELA:
- rela = ptr;
- r_offset = elf_uval(elf, rela, r_offset);
- r_info = elf_uval(elf, rela, r_info);
- r_addend = elf_uval(elf, rela, r_addend);
- break;
- default:
- /* can't happen */
- return -1;
- }
- if (elf_64bit(elf))
- {
- r_type = ELF64_R_TYPE(r_info);
- r_sym = ELF64_R_SYM(r_info);
- }
- else
- {
- r_type = ELF32_R_TYPE(r_info);
- r_sym = ELF32_R_SYM(r_info);
- }
-
- sym = elf_sym_by_index(elf, r_sym);
- shndx = elf_uval(elf, sym, st_shndx);
- switch (shndx)
- {
- case SHN_UNDEF:
- sname = "*UNDEF*";
- sbase = 0;
- break;
- case SHN_COMMON:
- elf_err(elf, "%s: invalid section: %" PRId64 "\n",
- __FUNCTION__, shndx);
- return -1;
- case SHN_ABS:
- sname = "*ABS*";
- sbase = 0;
- break;
- default:
- shdr = elf_shdr_by_index(elf, shndx);
- if (NULL == shdr)
- {
- elf_err(elf, "%s: invalid section: %" PRId64 "\n",
- __FUNCTION__, shndx);
- return -1;
- }
- sname = elf_section_name(elf, shdr);
- sbase = elf_uval(elf, shdr, sh_addr);
- }
-
- addr = r_offset;
- value = elf_uval(elf, sym, st_value);
- value += r_addend;
-
- if (elf->log && elf->verbose > 1)
- {
- uint64_t st_name = elf_uval(elf, sym, st_name);
- const char *name = st_name ? elf->sym_strtab + st_name : "*NONE*";
-
- elf_msg(elf,
- "%s: type %s [%d], off 0x%" PRIx64 ", add 0x%" PRIx64 ","
- " sym %s [0x%" PRIx64 "], sec %s [0x%" PRIx64 "]"
- " -> addr 0x%" PRIx64 " value 0x%" PRIx64 "\n",
- __FUNCTION__, rela_name(machine, r_type), r_type, r_offset,
- r_addend, name, elf_uval(elf, sym, st_value), sname, sbase,
- addr, value);
- }
-
- if (-1 == relocs[machine].func(elf, r_type, addr, value))
- {
- elf_err(elf, "%s: unknown/unsupported reloc type %s [%d]\n",
- __FUNCTION__, rela_name(machine, r_type), r_type);
- return -1;
- }
+ for ( ; ptr < end; ptr += rsize )
+ {
+ switch ( s_type )
+ {
+ case SHT_REL:
+ rel = ptr;
+ r_offset = elf_uval(elf, rel, r_offset);
+ r_info = elf_uval(elf, rel, r_info);
+ r_addend = 0;
+ break;
+ case SHT_RELA:
+ rela = ptr;
+ r_offset = elf_uval(elf, rela, r_offset);
+ r_info = elf_uval(elf, rela, r_info);
+ r_addend = elf_uval(elf, rela, r_addend);
+ break;
+ default:
+ /* can't happen */
+ return -1;
+ }
+ if ( elf_64bit(elf) )
+ {
+ r_type = ELF64_R_TYPE(r_info);
+ r_sym = ELF64_R_SYM(r_info);
+ }
+ else
+ {
+ r_type = ELF32_R_TYPE(r_info);
+ r_sym = ELF32_R_SYM(r_info);
+ }
+
+ sym = elf_sym_by_index(elf, r_sym);
+ shndx = elf_uval(elf, sym, st_shndx);
+ switch ( shndx )
+ {
+ case SHN_UNDEF:
+ sname = "*UNDEF*";
+ sbase = 0;
+ break;
+ case SHN_COMMON:
+ elf_err(elf, "%s: invalid section: %" PRId64 "\n",
+ __FUNCTION__, shndx);
+ return -1;
+ case SHN_ABS:
+ sname = "*ABS*";
+ sbase = 0;
+ break;
+ default:
+ shdr = elf_shdr_by_index(elf, shndx);
+ if ( shdr == NULL )
+ {
+ elf_err(elf, "%s: invalid section: %" PRId64 "\n",
+ __FUNCTION__, shndx);
+ return -1;
+ }
+ sname = elf_section_name(elf, shdr);
+ sbase = elf_uval(elf, shdr, sh_addr);
+ }
+
+ addr = r_offset;
+ value = elf_uval(elf, sym, st_value);
+ value += r_addend;
+
+ if ( elf->log && (elf->verbose > 1) )
+ {
+ uint64_t st_name = elf_uval(elf, sym, st_name);
+ const char *name = st_name ? elf->sym_strtab + st_name : "*NONE*";
+
+ elf_msg(elf,
+ "%s: type %s [%d], off 0x%" PRIx64 ", add 0x%" PRIx64 ","
+ " sym %s [0x%" PRIx64 "], sec %s [0x%" PRIx64 "]"
+ " -> addr 0x%" PRIx64 " value 0x%" PRIx64 "\n",
+ __FUNCTION__, rela_name(machine, r_type), r_type, r_offset,
+ r_addend, name, elf_uval(elf, sym, st_value), sname, sbase,
+ addr, value);
+ }
+
+ if ( relocs[machine].func(elf, r_type, addr, value) == -1 )
+ {
+ elf_err(elf, "%s: unknown/unsupported reloc type %s [%d]\n",
+ __FUNCTION__, rela_name(machine, r_type), r_type);
+ return -1;
+ }
}
return 0;
}
@@ -316,30 +319,40 @@ int elf_reloc(struct elf_binary *elf)
uint64_t i, count, type;
count = elf_shdr_count(elf);
- for (i = 0; i < count; i++)
- {
- rels = elf_shdr_by_index(elf, i);
- type = elf_uval(elf, rels, sh_type);
- if (type != SHT_REL && type != SHT_RELA)
- continue;
-
- sect = elf_shdr_by_index(elf, elf_uval(elf, rels, sh_info));
- syms = elf_shdr_by_index(elf, elf_uval(elf, rels, sh_link));
- if (NULL == sect || NULL == syms)
- continue;
-
- if (!(elf_uval(elf, sect, sh_flags) & SHF_ALLOC))
- {
- elf_msg(elf, "%s: relocations for %s, skipping\n",
- __FUNCTION__, elf_section_name(elf, sect));
- continue;
- }
-
- elf_msg(elf, "%s: relocations for %s @ 0x%" PRIx64 "\n",
- __FUNCTION__, elf_section_name(elf, sect),
- elf_uval(elf, sect, sh_addr));
- if (0 != elf_reloc_section(elf, rels, sect, syms))
- return -1;
- }
- return 0;
-}
+ for ( i = 0; i < count; i++ )
+ {
+ rels = elf_shdr_by_index(elf, i);
+ type = elf_uval(elf, rels, sh_type);
+ if ( (type != SHT_REL) && (type != SHT_RELA) )
+ continue;
+
+ sect = elf_shdr_by_index(elf, elf_uval(elf, rels, sh_info));
+ syms = elf_shdr_by_index(elf, elf_uval(elf, rels, sh_link));
+ if ( NULL == sect || NULL == syms )
+ continue;
+
+ if ( !(elf_uval(elf, sect, sh_flags) & SHF_ALLOC) )
+ {
+ elf_msg(elf, "%s: relocations for %s, skipping\n",
+ __FUNCTION__, elf_section_name(elf, sect));
+ continue;
+ }
+
+ elf_msg(elf, "%s: relocations for %s @ 0x%" PRIx64 "\n",
+ __FUNCTION__, elf_section_name(elf, sect),
+ elf_uval(elf, sect, sh_addr));
+ if ( elf_reloc_section(elf, rels, sect, syms) != 0 )
+ return -1;
+ }
+ return 0;
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-set-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff -r 3f63c1825b0d -r 58e71ae679d5 xen/common/libelf/libelf-tools.c
--- a/xen/common/libelf/libelf-tools.c Wed Feb 14 16:05:28 2007 +0000
+++ b/xen/common/libelf/libelf-tools.c Wed Feb 14 16:06:10 2007 +0000
@@ -7,7 +7,7 @@
/* ------------------------------------------------------------------------ */
uint64_t elf_access_unsigned(struct elf_binary * elf, const void *ptr,
- uint64_t offset, size_t size)
+ uint64_t offset, size_t size)
{
int need_swap = elf_swap(elf);
const uint8_t *u8;
@@ -15,27 +15,27 @@ uint64_t elf_access_unsigned(struct elf_
const uint32_t *u32;
const uint64_t *u64;
- switch (size)
- {
- case 1:
- u8 = ptr + offset;
- return *u8;
- case 2:
- u16 = ptr + offset;
- return need_swap ? bswap_16(*u16) : *u16;
- case 4:
- u32 = ptr + offset;
- return need_swap ? bswap_32(*u32) : *u32;
- case 8:
- u64 = ptr + offset;
- return need_swap ? bswap_64(*u64) : *u64;
- default:
- return 0;
+ switch ( size )
+ {
+ case 1:
+ u8 = ptr + offset;
+ return *u8;
+ case 2:
+ u16 = ptr + offset;
+ return need_swap ? bswap_16(*u16) : *u16;
+ case 4:
+ u32 = ptr + offset;
+ return need_swap ? bswap_32(*u32) : *u32;
+ case 8:
+ u64 = ptr + offset;
+ return need_swap ? bswap_64(*u64) : *u64;
+ default:
+ return 0;
}
}
int64_t elf_access_signed(struct elf_binary *elf, const void *ptr,
- uint64_t offset, size_t size)
+ uint64_t offset, size_t size)
{
int need_swap = elf_swap(elf);
const int8_t *s8;
@@ -43,22 +43,22 @@ int64_t elf_access_signed(struct elf_bin
const int32_t *s32;
const int64_t *s64;
- switch (size)
- {
- case 1:
- s8 = ptr + offset;
- return *s8;
- case 2:
- s16 = ptr + offset;
- return need_swap ? bswap_16(*s16) : *s16;
- case 4:
- s32 = ptr + offset;
- return need_swap ? bswap_32(*s32) : *s32;
- case 8:
- s64 = ptr + offset;
- return need_swap ? bswap_64(*s64) : *s64;
- default:
- return 0;
+ switch ( size )
+ {
+ case 1:
+ s8 = ptr + offset;
+ return *s8;
+ case 2:
+ s16 = ptr + offset;
+ return need_swap ? bswap_16(*s16) : *s16;
+ case 4:
+ s32 = ptr + offset;
+ return need_swap ? bswap_32(*s32) : *s32;
+ case 8:
+ s64 = ptr + offset;
+ return need_swap ? bswap_64(*s64) : *s64;
+ default:
+ return 0;
}
}
@@ -88,11 +88,12 @@ const elf_shdr *elf_shdr_by_name(struct
const char *sname;
int i;
- for (i = 0; i < count; i++) {
- shdr = elf_shdr_by_index(elf, i);
- sname = elf_section_name(elf, shdr);
- if (sname && 0 == strcmp(sname, name))
- return shdr;
+ for ( i = 0; i < count; i++ )
+ {
+ shdr = elf_shdr_by_index(elf, i);
+ sname = elf_section_name(elf, shdr);
+ if ( sname && !strcmp(sname, name) )
+ return shdr;
}
return NULL;
}
@@ -100,31 +101,35 @@ const elf_shdr *elf_shdr_by_index(struct
const elf_shdr *elf_shdr_by_index(struct elf_binary *elf, int index)
{
uint64_t count = elf_shdr_count(elf);
- const void *ptr = NULL;
-
- if (index < count)
- ptr = elf->image
- + elf_uval(elf, elf->ehdr, e_shoff)
- + elf_uval(elf, elf->ehdr, e_shentsize) * index;
+ const void *ptr;
+
+ if ( index >= count )
+ return NULL;
+
+ ptr = (elf->image
+ + elf_uval(elf, elf->ehdr, e_shoff)
+ + elf_uval(elf, elf->ehdr, e_shentsize) * index);
return ptr;
}
const elf_phdr *elf_phdr_by_index(struct elf_binary *elf, int index)
{
uint64_t count = elf_uval(elf, elf->ehdr, e_phnum);
- const void *ptr = NULL;
-
- if (index < count)
- ptr = elf->image
- + elf_uval(elf, elf->ehdr, e_phoff)
- + elf_uval(elf, elf->ehdr, e_phentsize) * index;
+ const void *ptr;
+
+ if ( index >= count )
+ return NULL;
+
+ ptr = (elf->image
+ + elf_uval(elf, elf->ehdr, e_phoff)
+ + elf_uval(elf, elf->ehdr, e_phentsize) * index);
return ptr;
}
const char *elf_section_name(struct elf_binary *elf, const elf_shdr * shdr)
{
- if (NULL == elf->sec_strtab)
- return "unknown";
+ if ( elf->sec_strtab == NULL )
+ return "unknown";
return elf->sec_strtab + elf_uval(elf, shdr, sh_name);
}
@@ -136,7 +141,7 @@ const void *elf_section_end(struct elf_b
const void *elf_section_end(struct elf_binary *elf, const elf_shdr * shdr)
{
return elf->image
- + elf_uval(elf, shdr, sh_offset) + elf_uval(elf, shdr, sh_size);
+ + elf_uval(elf, shdr, sh_offset) + elf_uval(elf, shdr, sh_size);
}
const elf_sym *elf_sym_by_name(struct elf_binary *elf, const char *symbol)
@@ -146,16 +151,16 @@ const elf_sym *elf_sym_by_name(struct el
const elf_sym *sym;
uint64_t info, name;
- for (; ptr < end; ptr += elf_size(elf, sym))
- {
- sym = ptr;
- info = elf_uval(elf, sym, st_info);
- name = elf_uval(elf, sym, st_name);
- if (ELF32_ST_BIND(info) != STB_GLOBAL)
- continue;
- if (strcmp(elf->sym_strtab + name, symbol) != 0)
- continue;
- return sym;
+ for ( ; ptr < end; ptr += elf_size(elf, sym) )
+ {
+ sym = ptr;
+ info = elf_uval(elf, sym, st_info);
+ name = elf_uval(elf, sym, st_name);
+ if ( ELF32_ST_BIND(info) != STB_GLOBAL )
+ continue;
+ if ( strcmp(elf->sym_strtab + name, symbol) )
+ continue;
+ return sym;
}
return NULL;
}
@@ -192,9 +197,9 @@ uint64_t elf_note_numeric(struct elf_bin
case 2:
case 4:
case 8:
- return elf_access_unsigned(elf, desc, 0, descsz);
- default:
- return 0;
+ return elf_access_unsigned(elf, desc, 0, descsz);
+ default:
+ return 0;
}
}
const elf_note *elf_note_next(struct elf_binary *elf, const elf_note * note)
@@ -211,9 +216,7 @@ int elf_is_elfbinary(const void *image)
{
const Elf32_Ehdr *ehdr = image;
- if (IS_ELF(*ehdr))
- return 1;
- return 0;
+ return IS_ELF(*ehdr);
}
int elf_phdr_is_loadable(struct elf_binary *elf, const elf_phdr * phdr)
@@ -223,3 +226,13 @@ int elf_phdr_is_loadable(struct elf_bina
return ((p_type == PT_LOAD) && (p_flags & (PF_W | PF_X)) != 0);
}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-set-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog
|