ia64/xen-unstable

view xen/tools/elf-reloc.c @ 652:784f6ddcee8e

bitkeeper revision 1.366.1.1 (3f1bbc10yTVXbX8BeEQcUVKQmXTucQ)

time.c:
Fix reading of CMOS time -- beware the update-in-progress flag.
author kaf24@scramble.cl.cam.ac.uk
date Mon Jul 21 10:10:24 2003 +0000 (2003-07-21)
parents 170eb7974e43
children 3aa0265ba99b
line source
1 /******************************************************************************
2 * elf-reloc.c
3 *
4 * Usage: elf-reloc <old base> <new base> <image>
5 *
6 * Relocates <image> from <old base> address to <new base> address by
7 * frobbing the Elf headers. Segment contents are unmodified!
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
14 typedef unsigned long Elf32_Addr;
15 typedef unsigned short Elf32_Half;
16 typedef unsigned long Elf32_Off;
17 typedef unsigned long Elf32_Word;
19 typedef struct {
20 unsigned char e_ident[16];
21 Elf32_Half e_type;
22 Elf32_Half e_machine;
23 Elf32_Word e_version;
24 Elf32_Addr e_entry;
25 Elf32_Off e_phoff;
26 Elf32_Off e_shoff;
27 Elf32_Word e_flags;
28 Elf32_Half e_ehsize;
29 Elf32_Half e_phentsize;
30 Elf32_Half e_phnum;
31 Elf32_Half e_shentsize;
32 Elf32_Half e_shnum;
33 Elf32_Half e_shstrndx;
34 } Elf32_Ehdr;
36 typedef struct {
37 Elf32_Word p_type;
38 Elf32_Off p_offset;
39 Elf32_Addr p_vaddr;
40 Elf32_Addr p_paddr;
41 Elf32_Word p_filesz;
42 Elf32_Word p_memsz;
43 Elf32_Word p_flags;
44 Elf32_Word p_align;
45 } Elf32_Phdr;
47 #define offsetof(_f,_p) ((unsigned long)&(((_p *)0)->_f))
50 /* Add @reloc_distance to address at offset @off in file @fp. */
51 void reloc(FILE *fp, long off, unsigned long reloc_distance)
52 {
53 unsigned long base;
54 fseek(fp, off, SEEK_SET);
55 fread(&base, sizeof(base), 1, fp);
56 base += reloc_distance;
57 fseek(fp, off, SEEK_SET);
58 fwrite(&base, sizeof(base), 1, fp);
60 }
63 int main(int argc, char **argv)
64 {
65 unsigned long old_base, new_base, reloc_distance;
66 long virt_section, phys_section;
67 char *image_name;
68 FILE *fp;
69 Elf32_Off phoff;
70 Elf32_Half phnum, phentsz;
71 int i;
73 if ( argc != 4 )
74 {
75 fprintf(stderr, "Usage: elf-reloc <old base> <new base> <image>\n");
76 return(1);
77 }
79 old_base = strtoul(argv[1], NULL, 16);
80 new_base = strtoul(argv[2], NULL, 16);
81 image_name = argv[3];
83 printf("Relocating `%s' from 0x%08lX to 0x%08lX\n",
84 image_name, old_base, new_base);
86 fp = fopen(image_name, "rb+");
87 if ( !fp )
88 {
89 fprintf(stderr, "Failed to load image!\n");
90 return(1);
91 }
93 reloc_distance = new_base - old_base;
95 /* First frob the entry address. */
96 reloc(fp, offsetof(e_entry, Elf32_Ehdr), reloc_distance);
98 fseek(fp, offsetof(e_phoff, Elf32_Ehdr), SEEK_SET);
99 fread(&phoff, sizeof(phoff), 1, fp);
100 fseek(fp, offsetof(e_phnum, Elf32_Ehdr), SEEK_SET);
101 fread(&phnum, sizeof(phnum), 1, fp);
102 fseek(fp, offsetof(e_phentsize, Elf32_Ehdr), SEEK_SET);
103 fread(&phentsz, sizeof(phentsz), 1, fp);
105 virt_section = (long)phoff + offsetof(p_vaddr, Elf32_Phdr);
106 phys_section = (long)phoff + offsetof(p_paddr, Elf32_Phdr);
107 for ( i = 0; i < phnum; i++ )
108 {
109 reloc(fp, phys_section, reloc_distance);
110 reloc(fp, virt_section, reloc_distance);
111 phys_section += phentsz;
112 virt_section += phentsz;
113 }
115 fclose(fp);
117 return(0);
118 }