6 #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
8 #define MAXSYMS (1 << 12)
9 #define MAXRELA (1 << 12)
19 static Elf32_Ehdr ehdr
;
20 static Elf32_Shdr shdr
[NSECS
];
21 static Elf32_Sym syms
[MAXSYMS
];
23 static char symstr
[MAXSYMS
* 8];
24 static int nsymstr
= 1;
26 static Elf32_Rel datrela
[MAXRELA
];
28 static Elf32_Rel rela
[MAXRELA
];
31 static char *putstr(char *s
, char *r
)
39 static int sym_find(char *name
)
42 for (i
= 0; i
< nsyms
; i
++)
43 if (!strcmp(name
, symstr
+ syms
[i
].st_name
))
48 static Elf32_Sym
*put_sym(char *name
)
50 int found
= sym_find(name
);
51 Elf32_Sym
*sym
= found
!= -1 ? &syms
[found
] : &syms
[nsyms
++];
54 sym
->st_name
= nsymstr
;
55 nsymstr
= putstr(symstr
+ nsymstr
, name
) - symstr
;
56 sym
->st_shndx
= SHN_UNDEF
;
57 sym
->st_info
= ELF32_ST_INFO(STB_GLOBAL
, STT_FUNC
);
61 #define SYMLOCAL(i) (ELF32_ST_BIND(syms[i].st_info) == STB_LOCAL)
63 static void mvrela(int *mv
, Elf32_Rel
*rela
, int nrela
)
66 for (i
= 0; i
< nrela
; i
++) {
67 int sym
= ELF32_R_SYM(rela
[i
].r_info
);
68 int type
= ELF32_R_TYPE(rela
[i
].r_info
);
69 rela
[i
].r_info
= ELF32_R_INFO(mv
[sym
], type
);
73 static int syms_sort(void)
78 for (i
= 0; i
< nsyms
; i
++)
84 while (i
< j
&& SYMLOCAL(i
))
86 while (j
>= i
&& !SYMLOCAL(j
))
97 mvrela(mv
, rela
, nrela
);
98 mvrela(mv
, datrela
, ndatrela
);
102 void out_init(int flags
)
106 void out_sym(char *name
, int flags
, int off
, int len
)
108 Elf32_Sym
*sym
= put_sym(name
);
109 int type
= (flags
& OUT_CS
) ? STT_FUNC
: STT_OBJECT
;
110 int bind
= (flags
& OUT_GLOB
) ? STB_GLOBAL
: STB_LOCAL
;
112 sym
->st_shndx
= SEC_TEXT
;
114 sym
->st_shndx
= SEC_DAT
;
116 sym
->st_shndx
= SEC_BSS
;
117 sym
->st_info
= ELF32_ST_INFO(bind
, type
);
122 static int rel_type(int flags
)
124 if (flags
& OUT_REL24
)
131 static void out_csrel(int idx
, int off
, int flags
)
133 Elf32_Rel
*r
= &rela
[nrela
++];
135 r
->r_info
= ELF32_R_INFO(idx
, rel_type(flags
));
138 static void out_dsrel(int idx
, int off
, int flags
)
140 Elf32_Rel
*r
= &datrela
[ndatrela
++];
142 r
->r_info
= ELF32_R_INFO(idx
, rel_type(flags
));
145 void out_rel(char *name
, int flags
, int off
)
147 Elf32_Sym
*sym
= put_sym(name
);
148 int idx
= sym
- syms
;
150 out_dsrel(idx
, off
, flags
);
152 out_csrel(idx
, off
, flags
);
155 static int bss_len(void)
159 for (i
= 0; i
< nsyms
; i
++) {
160 int end
= syms
[i
].st_value
+ syms
[i
].st_size
;
161 if (syms
[i
].st_shndx
== SEC_BSS
)
168 void out_write(int fd
, char *cs
, int cslen
, char *ds
, int dslen
)
170 Elf32_Shdr
*text_shdr
= &shdr
[SEC_TEXT
];
171 Elf32_Shdr
*rela_shdr
= &shdr
[SEC_RELA
];
172 Elf32_Shdr
*symstr_shdr
= &shdr
[SEC_SYMSTR
];
173 Elf32_Shdr
*syms_shdr
= &shdr
[SEC_SYMS
];
174 Elf32_Shdr
*dat_shdr
= &shdr
[SEC_DAT
];
175 Elf32_Shdr
*datrela_shdr
= &shdr
[SEC_DATRELA
];
176 Elf32_Shdr
*bss_shdr
= &shdr
[SEC_BSS
];
177 unsigned long offset
= sizeof(ehdr
);
179 ehdr
.e_ident
[0] = 0x7f;
180 ehdr
.e_ident
[1] = 'E';
181 ehdr
.e_ident
[2] = 'L';
182 ehdr
.e_ident
[3] = 'F';
183 ehdr
.e_ident
[4] = ELFCLASS32
;
184 ehdr
.e_ident
[5] = ELFDATA2LSB
;
185 ehdr
.e_ident
[6] = EV_CURRENT
;
186 ehdr
.e_type
= ET_REL
;
187 ehdr
.e_machine
= EM_ARM
;
188 ehdr
.e_flags
= EF_ARM_EABI_VER4
;
189 ehdr
.e_version
= EV_CURRENT
;
190 ehdr
.e_ehsize
= sizeof(ehdr
);
191 ehdr
.e_shentsize
= sizeof(shdr
[0]);
192 ehdr
.e_shoff
= offset
;
193 ehdr
.e_shnum
= NSECS
;
194 ehdr
.e_shstrndx
= SEC_SYMSTR
;
195 offset
+= sizeof(shdr
[0]) * NSECS
;
197 text_shdr
->sh_type
= SHT_PROGBITS
;
198 text_shdr
->sh_flags
= SHF_EXECINSTR
| SHF_ALLOC
;
199 text_shdr
->sh_offset
= offset
;
200 text_shdr
->sh_size
= cslen
;
201 text_shdr
->sh_entsize
= 1;
202 offset
+= text_shdr
->sh_size
;
204 rela_shdr
->sh_type
= SHT_REL
;
205 rela_shdr
->sh_link
= SEC_SYMS
;
206 rela_shdr
->sh_info
= SEC_TEXT
;
207 rela_shdr
->sh_offset
= offset
;
208 rela_shdr
->sh_size
= nrela
* sizeof(rela
[0]);
209 rela_shdr
->sh_entsize
= sizeof(rela
[0]);
210 offset
+= rela_shdr
->sh_size
;
212 syms_shdr
->sh_type
= SHT_SYMTAB
;
213 syms_shdr
->sh_offset
= offset
;
214 syms_shdr
->sh_size
= nsyms
* sizeof(syms
[0]);
215 syms_shdr
->sh_entsize
= sizeof(syms
[0]);
216 syms_shdr
->sh_link
= SEC_SYMSTR
;
217 syms_shdr
->sh_info
= syms_sort();
218 offset
+= syms_shdr
->sh_size
;
220 dat_shdr
->sh_type
= SHT_PROGBITS
;
221 dat_shdr
->sh_flags
= SHF_ALLOC
| SHF_WRITE
;
222 dat_shdr
->sh_offset
= offset
;
223 dat_shdr
->sh_size
= dslen
;
224 dat_shdr
->sh_entsize
= 1;
225 dat_shdr
->sh_addralign
= 4;
226 offset
+= dat_shdr
->sh_size
;
228 datrela_shdr
->sh_type
= SHT_REL
;
229 datrela_shdr
->sh_offset
= offset
;
230 datrela_shdr
->sh_size
= ndatrela
* sizeof(datrela
[0]);
231 datrela_shdr
->sh_entsize
= sizeof(datrela
[0]);
232 datrela_shdr
->sh_link
= SEC_SYMS
;
233 datrela_shdr
->sh_info
= SEC_DAT
;
234 offset
+= datrela_shdr
->sh_size
;
236 bss_shdr
->sh_type
= SHT_NOBITS
;
237 bss_shdr
->sh_flags
= SHF_ALLOC
| SHF_WRITE
;
238 bss_shdr
->sh_offset
= offset
;
239 bss_shdr
->sh_size
= bss_len();
240 bss_shdr
->sh_entsize
= 1;
241 bss_shdr
->sh_addralign
= 4;
243 symstr_shdr
->sh_type
= SHT_STRTAB
;
244 symstr_shdr
->sh_offset
= offset
;
245 symstr_shdr
->sh_size
= nsymstr
;
246 symstr_shdr
->sh_entsize
= 1;
247 offset
+= symstr_shdr
->sh_size
;
249 write(fd
, &ehdr
, sizeof(ehdr
));
250 write(fd
, shdr
, NSECS
* sizeof(shdr
[0]));
251 write(fd
, cs
, cslen
);
252 write(fd
, rela
, nrela
* sizeof(rela
[0]));
253 write(fd
, syms
, nsyms
* sizeof(syms
[0]));
254 write(fd
, ds
, dslen
);
255 write(fd
, datrela
, ndatrela
* sizeof(datrela
[0]));
256 write(fd
, symstr
, nsymstr
);