@@ -134,8 +134,9 @@ void elf_generate_header(void)
134134 hdr .e_version = 1 ; /* ELF version */
135135 hdr .e_entry = ELF_START + elf_header_len ; /* entry point */
136136 hdr .e_phoff = 0x34 ; /* program header offset */
137- hdr .e_shoff = elf_header_len + elf_code -> size + elf_data -> size + 39 +
138- elf_symtab -> size +
137+ /* Compute section header offset dynamically */
138+ hdr .e_shoff = elf_header_len + elf_code -> size + elf_data -> size +
139+ elf_rodata -> size + elf_symtab -> size +
139140 elf_strtab -> size ; /* section header offset */
140141 hdr .e_flags = ELF_FLAGS ; /* flags */
141142 hdr .e_ehsize [0 ] = (char ) 0x34 ; /* header size */
@@ -146,9 +147,9 @@ void elf_generate_header(void)
146147 hdr .e_phnum [1 ] = 0 ;
147148 hdr .e_shentsize [0 ] = (char ) 0x28 ; /* section header size */
148149 hdr .e_shentsize [1 ] = 0 ;
149- hdr .e_shnum [0 ] = 6 ; /* number of section headers */
150+ hdr .e_shnum [0 ] = 8 ; /* number of section headers - added .bss and .rodata */
150151 hdr .e_shnum [1 ] = 0 ;
151- hdr .e_shstrndx [0 ] = 5 ; /* section index with names */
152+ hdr .e_shstrndx [0 ] = 7 ; /* section index with names */
152153 hdr .e_shstrndx [1 ] = 0 ;
153154 elf_write_blk (elf_header , & hdr , sizeof (elf32_hdr_t ));
154155
@@ -176,14 +177,18 @@ void elf_generate_header(void)
176177 */
177178 /* program header - code and data combined */
178179 elf32_phdr_t phdr ;
179- phdr .p_type = 1 ; /* PT_LOAD */
180- phdr .p_offset = elf_header_len ; /* offset of segment */
181- phdr .p_vaddr = ELF_START + elf_header_len ; /* virtual address */
182- phdr .p_paddr = ELF_START + elf_header_len ; /* physical address */
183- phdr .p_filesz = elf_code -> size + elf_data -> size ; /* size in file */
184- phdr .p_memsz = elf_code -> size + elf_data -> size ; /* size in memory */
185- phdr .p_flags = 7 ; /* flags */
186- phdr .p_align = 4 ; /* alignment */
180+ phdr .p_type = 1 ; /* PT_LOAD */
181+ phdr .p_offset = elf_header_len ; /* offset of segment */
182+ phdr .p_vaddr = ELF_START + elf_header_len ; /* virtual address */
183+ phdr .p_paddr = ELF_START + elf_header_len ; /* physical address */
184+ /* size in file */
185+ phdr .p_filesz = elf_code -> size + elf_data -> size + elf_rodata -> size ;
186+ /* Include .bss size in memory but not in file size */
187+ phdr .p_memsz =
188+ elf_code -> size + elf_data -> size + elf_rodata -> size + elf_bss_size ;
189+ /* PF_X | PF_W | PF_R (all permissions for compatibility) */
190+ phdr .p_flags = 7 ;
191+ phdr .p_align = 4 ; /* alignment */
187192 elf_write_blk (elf_header , & phdr , sizeof (elf32_phdr_t ));
188193}
189194
@@ -203,18 +208,24 @@ void elf_generate_sections(void)
203208 for (int b = 0 ; b < elf_strtab -> size ; b ++ )
204209 elf_write_byte (elf_section , elf_strtab -> elements [b ]);
205210
206- /* shstr section; len = 39 */
211+ /* shstr section - compute size dynamically */
212+ int shstrtab_start = elf_section -> size ;
207213 elf_write_byte (elf_section , 0 );
208214 elf_write_str (elf_section , ".shstrtab" );
209215 elf_write_byte (elf_section , 0 );
210216 elf_write_str (elf_section , ".text" );
211217 elf_write_byte (elf_section , 0 );
212218 elf_write_str (elf_section , ".data" );
213219 elf_write_byte (elf_section , 0 );
220+ elf_write_str (elf_section , ".rodata" );
221+ elf_write_byte (elf_section , 0 );
222+ elf_write_str (elf_section , ".bss" );
223+ elf_write_byte (elf_section , 0 );
214224 elf_write_str (elf_section , ".symtab" );
215225 elf_write_byte (elf_section , 0 );
216226 elf_write_str (elf_section , ".strtab" );
217227 elf_write_byte (elf_section , 0 );
228+ int shstrtab_size = elf_section -> size - shstrtab_start ;
218229
219230 /* section header table */
220231 elf32_shdr_t shdr ;
@@ -288,22 +299,51 @@ void elf_generate_sections(void)
288299 elf_write_blk (elf_section , & shdr , sizeof (elf32_shdr_t ));
289300 ofs += elf_data -> size ;
290301
291- /* .symtab */
302+ /* .rodata */
292303 shdr .sh_name = 0x17 ;
304+ shdr .sh_type = 1 ;
305+ shdr .sh_flags = 2 ; /* SHF_ALLOC only, no SHF_WRITE */
306+ shdr .sh_addr = elf_code_start + elf_code -> size + elf_data -> size ;
307+ shdr .sh_offset = ofs ;
308+ shdr .sh_size = elf_rodata -> size ;
309+ shdr .sh_link = 0 ;
310+ shdr .sh_info = 0 ;
311+ shdr .sh_addralign = 4 ;
312+ shdr .sh_entsize = 0 ;
313+ elf_write_blk (elf_section , & shdr , sizeof (elf32_shdr_t ));
314+ ofs += elf_rodata -> size ;
315+
316+ /* .bss */
317+ shdr .sh_name = 0x1f ;
318+ shdr .sh_type = 8 ; /* SHT_NOBITS */
319+ shdr .sh_flags = 3 ; /* SHF_WRITE | SHF_ALLOC */
320+ shdr .sh_addr =
321+ elf_code_start + elf_code -> size + elf_data -> size + elf_rodata -> size ;
322+ shdr .sh_offset = ofs ; /* No file space for .bss */
323+ shdr .sh_size = elf_bss_size ;
324+ shdr .sh_link = 0 ;
325+ shdr .sh_info = 0 ;
326+ shdr .sh_addralign = 4 ;
327+ shdr .sh_entsize = 0 ;
328+ elf_write_blk (elf_section , & shdr , sizeof (elf32_shdr_t ));
329+ /* Note: .bss doesn't increase file offset */
330+
331+ /* .symtab */
332+ shdr .sh_name = 0x24 ;
293333 shdr .sh_type = 2 ;
294334 shdr .sh_flags = 0 ;
295335 shdr .sh_addr = 0 ;
296336 shdr .sh_offset = ofs ;
297337 shdr .sh_size = elf_symtab -> size ;
298- shdr .sh_link = 4 ;
338+ shdr .sh_link = 6 ;
299339 shdr .sh_info = elf_symbol_index ;
300340 shdr .sh_addralign = 4 ;
301341 shdr .sh_entsize = 16 ;
302342 elf_write_blk (elf_section , & shdr , sizeof (elf32_shdr_t ));
303343 ofs += elf_symtab -> size ;
304344
305345 /* .strtab */
306- shdr .sh_name = 0x1f ;
346+ shdr .sh_name = 0x2c ;
307347 shdr .sh_type = 3 ;
308348 shdr .sh_flags = 0 ;
309349 shdr .sh_addr = 0 ;
@@ -322,7 +362,7 @@ void elf_generate_sections(void)
322362 shdr .sh_flags = 0 ;
323363 shdr .sh_addr = 0 ;
324364 shdr .sh_offset = ofs ;
325- shdr .sh_size = 39 ;
365+ shdr .sh_size = shstrtab_size ;
326366 shdr .sh_link = 0 ;
327367 shdr .sh_info = 0 ;
328368 shdr .sh_addralign = 1 ;
@@ -333,14 +373,17 @@ void elf_generate_sections(void)
333373void elf_align (void )
334374{
335375 /* Check for null pointers to prevent crashes */
336- if (!elf_data || !elf_symtab || !elf_strtab ) {
376+ if (!elf_data || !elf_rodata || ! elf_symtab || !elf_strtab ) {
337377 error ("ELF buffers not initialized for alignment" );
338378 return ;
339379 }
340380
341381 while (elf_data -> size & 3 )
342382 elf_write_byte (elf_data , 0 );
343383
384+ while (elf_rodata -> size & 3 )
385+ elf_write_byte (elf_rodata , 0 );
386+
344387 while (elf_symtab -> size & 3 )
345388 elf_write_byte (elf_symtab , 0 );
346389
@@ -387,6 +430,8 @@ void elf_generate(char *outfile)
387430 fputc (elf_code -> elements [i ], fp );
388431 for (int i = 0 ; i < elf_data -> size ; i ++ )
389432 fputc (elf_data -> elements [i ], fp );
433+ for (int i = 0 ; i < elf_rodata -> size ; i ++ )
434+ fputc (elf_rodata -> elements [i ], fp );
390435 for (int i = 0 ; i < elf_section -> size ; i ++ )
391436 fputc (elf_section -> elements [i ], fp );
392437 fclose (fp );
0 commit comments