1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 
6 /* Define the file handles.  */
7 
8 FILE	*source_file;
9 FILE    *binary_file;
10 
11 
12 #define ELF_ID_STRING_SIZE	    16
13 #define ELF_ARM_MACHINE_TYPE    40
14 #define ELF_EXECUTABLE          2
15 
16 
17 typedef struct ELF_HEADER_STRUCT
18 {
19     unsigned char   elf_header_id_string[ELF_ID_STRING_SIZE];
20     unsigned short  elf_header_file_type;
21     unsigned short  elf_header_machinge_type;
22     unsigned long   elf_header_version;
23     unsigned long   elf_header_entry_address;
24     unsigned long   elf_header_program_header_offset;
25     unsigned long   elf_header_section_header_offset;
26     unsigned long   elf_header_processor_flags;
27     unsigned short  elf_header_size;
28     unsigned short  elf_header_program_header_size;
29     unsigned short  elf_header_program_header_entries;
30     unsigned short  elf_header_section_header_size;
31     unsigned short  elf_header_section_header_entries;
32     unsigned short  elf_header_section_string_index;
33 } ELF_HEADER;
34 
35 
36 typedef struct ELF_PROGRAM_HEADER_STRUCT
37 {
38     unsigned long   elf_program_header_type;
39     unsigned long   elf_program_header_offset;
40     unsigned long   elf_program_header_virtual_address;
41     unsigned long   elf_program_header_physical_address;
42     unsigned long   elf_program_header_file_size;
43     unsigned long   elf_program_header_memory_size;
44     unsigned long   elf_program_header_flags;
45     unsigned long   elf_program_header_alignment;
46 } ELF_PROGRAM_HEADER;
47 
48 
49 typedef struct ELF_SECTION_HEADER_STRUCT
50 {
51     unsigned long   elf_section_header_name;
52     unsigned long   elf_section_header_type;
53     unsigned long   elf_section_header_flags;
54     unsigned long   elf_section_header_address;
55     unsigned long   elf_section_header_offset;
56     unsigned long   elf_section_header_size;
57     unsigned long   elf_section_header_link;
58     unsigned long   elf_section_header_info;
59     unsigned long   elf_section_header_alignment;
60     unsigned long   elf_section_header_entry_size;
61 } ELF_SECTION_HEADER;
62 
63 
64 typedef struct ELF_SYMBOL_TABLE_ENTRY_STRUCT
65 {
66     unsigned long   elf_symbol_table_entry_name;
67     unsigned long   elf_symbol_table_entry_address;
68     unsigned long   elf_symbol_table_entry_size;
69     unsigned char   elf_symbol_table_entry_info;
70     unsigned char   elf_symbol_table_entry_other;
71     unsigned short  elf_symbol_table_entry_shndx;
72 
73 } ELF_SYMBOL_TABLE_ENTRY;
74 
75 
76 typedef struct CODE_SECTION_ENTRY_STRUCT
77 {
78     unsigned long   code_section_index;
79     unsigned long   code_section_address;
80     unsigned long   code_section_size;
81 } CODE_SECTION_ENTRY;
82 
83 
84 /* Define global variables.  */
85 
86 ELF_HEADER              header;
87 ELF_PROGRAM_HEADER      *program_header;
88 ELF_SECTION_HEADER      *section_header;
89 unsigned char           *section_string_table;
90 unsigned char           *main_string_table;
91 unsigned long           total_symbols;
92 ELF_SYMBOL_TABLE_ENTRY  *symbol_table;
93 unsigned long           total_functions;
94 ELF_SYMBOL_TABLE_ENTRY  *function_table;
95 CODE_SECTION_ENTRY		*code_section_array;
96 
97 
98 /* Define helper functions.  */
99 
elf_object_read(unsigned long offset,void * object_address,int object_size)100 int elf_object_read(unsigned long offset, void *object_address, int object_size)
101 {
102 
103 int             i;
104 int             alpha;
105 unsigned char   *buffer;
106 
107     /* Setup the buffer pointer.  */
108     buffer =  (unsigned char *) object_address;
109 
110     /* Seek to the proper position in the file.  */
111     fseek(source_file, offset, SEEK_SET);
112 
113     /* Read the ELF object.  */
114     for (i = 0; i < object_size; i++)
115     {
116         alpha =  fgetc(source_file);
117 
118         if (alpha == EOF)
119             return(1);
120 
121         buffer[i] =  (unsigned char) alpha;
122     }
123 
124     /* Return success.  */
125     return(0);
126 }
127 
128 
main(int argc,char * argv[])129 int main(int argc, char* argv[])
130 {
131 
132 unsigned long           i, j;
133 unsigned long           current_total;
134 unsigned long           address;
135 unsigned long           size;
136 unsigned char           *code_buffer;
137 unsigned long			code_section_index;
138 CODE_SECTION_ENTRY		code_section_temp;
139 unsigned char           zero_value;
140 
141 
142     /* Determine if the proper number of files are provided.  */
143     if (argc != 3)
144     {
145 
146         /* Print an error message out and wait for user key hit.  */
147 		printf("module_to_binary.exe - Copyright (c) Microsoft Corporation v5.8\n");
148         printf("**** Error: invalid input parameter for module_to_binary.exe **** \n");
149         printf("     Command Line Should be:\n\n");
150         printf("     > module_to_binary source_elf_file c_binary_file <cr> \n\n");
151         return(1);
152     }
153 
154     /* Attempt to open the source file for reading.  */
155     source_file =  fopen(argv[1], "rb");
156 
157     /* Determine if the source file was opened properly.  */
158     if (source_file == NULL)
159     {
160 
161         /* Print an error message out.  */
162         printf("**** Error: open failed on source elf file **** \n");
163         printf("            File: %s   ", argv[1]);
164         return(2);
165     }
166 
167     /* Attempt to open the binary file for writing.  */
168     binary_file =  fopen(argv[2], "wb");
169 
170     /* Determine if the binary file was opened properly.  */
171     if (binary_file == NULL)
172     {
173 
174         /* Print an error message out and wait for user key hit.  */
175         printf("**** Error: open failed on binary output file **** \n");
176         printf("            File: %s   ", argv[2]);
177         return(3);
178     }
179 
180     /* Read the ELF header.  */
181     elf_object_read(0, &header, sizeof(header));
182 
183     /* Allocate memory for the program header(s).  */
184     program_header =  malloc(sizeof(ELF_PROGRAM_HEADER)*header.elf_header_program_header_entries);
185 
186     /* Read the program header(s).  */
187     elf_object_read(header.elf_header_program_header_offset, program_header, (sizeof(ELF_PROGRAM_HEADER)*header.elf_header_program_header_entries));
188 
189     /* Allocate memory for the section header(s).  */
190     section_header =  malloc(sizeof(ELF_SECTION_HEADER)*header.elf_header_section_header_entries);
191 
192     /* Read the section header(s).  */
193     elf_object_read(header.elf_header_section_header_offset, section_header, (sizeof(ELF_SECTION_HEADER)*header.elf_header_section_header_entries));
194 
195 
196     /* Alocate memory for the section string table.  */
197     section_string_table =  malloc(section_header[header.elf_header_section_string_index].elf_section_header_size);
198 
199     /* Read the section string table.  */
200     elf_object_read(section_header[header.elf_header_section_string_index].elf_section_header_offset, section_string_table, section_header[header.elf_header_section_string_index].elf_section_header_size);
201 
202     /* Allocate memory for the code section array.  */
203     code_section_array =  malloc(sizeof(CODE_SECTION_ENTRY)*header.elf_header_section_header_entries);
204 	code_section_index =  0;
205 
206      /* Print out the section header(s).  */
207     for (i = 0; i < header.elf_header_section_header_entries; i++)
208     {
209 
210 		/* Determine if this section is a code section and there is a size.  */
211 		if ((section_header[i].elf_section_header_type == 1) && (section_header[i].elf_section_header_size))
212 		{
213 
214         /* Check for an-instruction area.  */
215         if ((section_header[i].elf_section_header_flags & 0x4) || (section_header[i].elf_section_header_flags & 0x2))
216 			{
217 
218                 /* Determine if this new section overlaps with an existing section.  */
219 				for (j = 0; j < code_section_index; j++)
220 				{
221 					/* Is there an overlap?  */
222 					if ((section_header[i].elf_section_header_address >= code_section_array[j].code_section_address) &&
223 						((section_header[i].elf_section_header_address+section_header[i].elf_section_header_size + section_header[i].elf_section_header_offset) < (code_section_array[j].code_section_address+code_section_array[j].code_section_size)))
224 					{
225 						/* New section is within a current section, just disregard it.  */
226 						break;
227 					}
228 				}
229 
230 				/* Determine if we have an overlap.  */
231 				if (j == code_section_index)
232 				{
233 
234 				    /* Yes, we have a code section... save it!  */
235 				    code_section_array[code_section_index].code_section_index =  i;
236 				    code_section_array[code_section_index].code_section_address =  section_header[i].elf_section_header_address;
237 				    code_section_array[code_section_index].code_section_size =     section_header[i].elf_section_header_size;
238 
239 				    /* Move to next code section.  */
240 				    code_section_index++;
241 				}
242 			}
243 		}
244 	}
245 
246 	/* Check for no code sections.  */
247 	if (code_section_index == 0)
248 	{
249 
250 		/* Close files.  */
251 		fclose(source_file);
252 		fclose(binary_file);
253 
254 		return(4);
255  	}
256 
257 	/* One or more code sections have been found... let's put them in the correct order by address.  */
258 	i = 0;
259 	while (i+1 < code_section_index)
260 	{
261 
262 		/* Make the "ith" entry the lowest address.  */
263 		j = i + 1;
264 		do
265 		{
266 			/* Is there a new lowest address?  */
267 			if (code_section_array[j].code_section_address < code_section_array[i].code_section_address)
268 			{
269 				/* Yes, swap them!  */
270 				code_section_temp =  code_section_array[i];
271 				code_section_array[i] =  code_section_array[j];
272 				code_section_array[j] = code_section_temp;
273 			}
274 
275 			/* Move the inner index.  */
276 			j++;
277 		} while (j < code_section_index);
278 
279 		/* Move top index.  */
280 		i++;
281 	}
282 
283 	address =  code_section_array[0].code_section_address;
284 	zero_value = 0;
285 	for (i = 0; i < code_section_index; i++)
286 	{
287 
288 		/* Determine if there is any fill characters between sections.  */
289 		while (address < code_section_array[i].code_section_address)
290 		{
291 
292 			/* Write a zero value.  */
293 			fwrite(&zero_value, 1, 1, binary_file);
294 
295 			/* Move address forward.  */
296 			address++;
297 		}
298 
299 		/* Now allocate memory for the code section.  */
300 		code_buffer =  malloc(code_section_array[i].code_section_size);
301 
302 		/* Read in the code area.  */
303 		j =  code_section_array[i].code_section_index;
304 		elf_object_read(section_header[j].elf_section_header_offset, code_buffer, code_section_array[i].code_section_size);
305 
306 		/* Write out the contents of this program area.  */
307 		size =  code_section_array[i].code_section_size;
308 
309 		j =  0;
310 		while (size)
311 		{
312 
313 			/* Print out a byte.  */
314 			fwrite(&code_buffer[j], 1, 1, binary_file);
315 
316 			/* Move address forward.  */
317 			address++;
318 
319 			/* Decrement size.  */
320 			size--;
321 
322 			/* Move index into buffer.  */
323 			j++;
324 		}
325 	}
326 
327 	/* Close files.  */
328 	fclose(source_file);
329     fclose(binary_file);
330 
331 	return 0;
332 }
333