1 /*-------------------------------------------------------------------------
2 lkelf.c - Create an executable ELF/DWARF file
4 Written By - Erik Petrich, epetrich@users.sourceforge.net (2004)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 -------------------------------------------------------------------------*/
28 # ifdef __MINGW32__ /* GCC MINGW32 depends on configure */
29 # include "sdccconf.h"
33 #else /* Assume Un*x style system */
34 # include "sdccconf.h"
37 static int execStartMSB;
38 static int execStartLSB;
39 static char execStartMSBfound;
40 static char execStartLSBfound;
42 typedef TYPE_UDWORD Elf32_Addr;
43 typedef TYPE_WORD Elf32_Half;
44 typedef TYPE_UDWORD Elf32_Off;
45 typedef TYPE_DWORD Elf32_Sword;
46 typedef TYPE_UDWORD Elf32_Word;
78 /* These e_machine values are from "Motorola 8- and 16-bit Embedded */
79 /* Application Binary Interface (M8/16EABI)" version 2.0 */
128 SHF_WRITE = (1 << 0),
129 SHF_ALLOC = (1 << 1),
130 SHF_EXECINSTR = (1 << 2),
148 unsigned char e_ident[EI_NIDENT];
150 Elf32_Half e_machine;
151 Elf32_Word e_version;
157 Elf32_Half e_phentsize;
159 Elf32_Half e_shentsize;
161 Elf32_Half e_shstrndx;
174 Elf32_Word sh_addralign;
175 Elf32_Word sh_entsize;
190 typedef struct strtabString
193 struct strtabString * prev;
194 struct strtabString * next;
200 strtabString * first;
204 static strtabList shstrtab;
207 typedef struct listEntry
210 struct listEntry * prev;
211 struct listEntry * next;
224 listAdd (listHeader * lhp, void * item)
228 lep = new (sizeof (*lep));
230 lep->prev = lhp->last;
232 lep->prev->next = lep;
246 lhp = new (sizeof (*lhp));
254 strtabFind (strtabList * strtab, char * str)
261 if (!strcmp (str, sp->string))
270 /*-------------------------------------------------------------------*/
271 /* strtabFindOrAdd - Finds a string in a string table or adds the */
272 /* string if it does not already exist. Returns the offset of the */
273 /* string in the table. */
274 /*-------------------------------------------------------------------*/
276 strtabFindOrAdd (strtabList * strtab, char * str)
283 if (!strcmp (str, sp->string))
288 sp = new (sizeof(*sp));
290 sp->index = strtab->last->index + 1 + strlen (strtab->last->string);
293 sp->string = new (1+strlen (str));
294 strcpy (sp->string, str);
296 sp->prev = strtab->last;
306 /*-------------------------------------------------------------------*/
307 /* fputElfStrtab - writes a string table to a file */
308 /*-------------------------------------------------------------------*/
310 fputElfStrtab (strtabList *strtab, FILE *fp)
314 fputc (0, fp); /* index 0 must be the null character */
319 fputs (sp->string, fp);
325 /*-------------------------------------------------------------------*/
326 /* fputElf32_Word - writes an Elf32_Word value to a file */
327 /*-------------------------------------------------------------------*/
329 fputElf32_Word (Elf32_Word x, FILE *fp)
333 fputc (x & 0xff, fp);
334 fputc ((x >> 8) & 0xff, fp);
335 fputc ((x >> 16) & 0xff, fp);
336 fputc ((x >> 24) & 0xff, fp);
340 fputc ((x >> 24) & 0xff, fp);
341 fputc ((x >> 16) & 0xff, fp);
342 fputc ((x >> 8) & 0xff, fp);
343 fputc (x & 0xff, fp);
347 /*-------------------------------------------------------------------*/
348 /* fputElf32_Off - writes an Elf32_Off value to a file */
349 /*-------------------------------------------------------------------*/
351 fputElf32_Off (Elf32_Off x, FILE *fp)
355 fputc (x & 0xff, fp);
356 fputc ((x >> 8) & 0xff, fp);
357 fputc ((x >> 16) & 0xff, fp);
358 fputc ((x >> 24) & 0xff, fp);
362 fputc ((x >> 24) & 0xff, fp);
363 fputc ((x >> 16) & 0xff, fp);
364 fputc ((x >> 8) & 0xff, fp);
365 fputc (x & 0xff, fp);
369 /*-------------------------------------------------------------------*/
370 /* fputElf32_Addr - writes an Elf32_Addr value to a file */
371 /*-------------------------------------------------------------------*/
373 fputElf32_Addr (Elf32_Addr x, FILE *fp)
377 fputc (x & 0xff, fp);
378 fputc ((x >> 8) & 0xff, fp);
379 fputc ((x >> 16) & 0xff, fp);
380 fputc ((x >> 24) & 0xff, fp);
384 fputc ((x >> 24) & 0xff, fp);
385 fputc ((x >> 16) & 0xff, fp);
386 fputc ((x >> 8) & 0xff, fp);
387 fputc (x & 0xff, fp);
391 /*-------------------------------------------------------------------*/
392 /* fputElf32_Half - writes an Elf32_Half value to a file */
393 /*-------------------------------------------------------------------*/
395 fputElf32_Half (Elf32_Half x, FILE *fp)
399 fputc (x & 0xff, fp);
400 fputc ((x >> 8) & 0xff, fp);
404 fputc ((x >> 8) & 0xff, fp);
405 fputc (x & 0xff, fp);
409 /*------------------------------------------------------------------------*/
410 /* fputElf32_Ehdr - writes an Elf32_Ehdr struct (ELF header) to a file */
411 /*------------------------------------------------------------------------*/
413 fputElf32_Ehdr (Elf32_Ehdr * ehdr, FILE * fp)
417 for (i=0; i<EI_NIDENT; i++)
418 fputc (ehdr->e_ident[i], fp);
420 fputElf32_Half (ehdr->e_type, fp);
421 fputElf32_Half (ehdr->e_machine, fp);
422 fputElf32_Word (ehdr->e_version, fp);
423 fputElf32_Addr (ehdr->e_entry, fp);
424 fputElf32_Off (ehdr->e_phoff, fp);
425 fputElf32_Off (ehdr->e_shoff, fp);
426 fputElf32_Word (ehdr->e_flags, fp);
427 fputElf32_Half (ehdr->e_ehsize, fp);
428 fputElf32_Half (ehdr->e_phentsize, fp);
429 fputElf32_Half (ehdr->e_phnum, fp);
430 fputElf32_Half (ehdr->e_shentsize, fp);
431 fputElf32_Half (ehdr->e_shnum, fp);
432 fputElf32_Half (ehdr->e_shstrndx, fp);
435 /*-------------------------------------------------------------------------*/
436 /* fputElf32_Ehdr - writes an Elf32_Shdr struct (section header) to a file */
437 /*-------------------------------------------------------------------------*/
439 fputElf32_Shdr (Elf32_Shdr * shdr, FILE * fp)
441 fputElf32_Word (shdr->sh_name, fp);
442 fputElf32_Word (shdr->sh_type, fp);
443 fputElf32_Word (shdr->sh_flags, fp);
444 fputElf32_Addr (shdr->sh_addr, fp);
445 fputElf32_Off (shdr->sh_offset, fp);
446 fputElf32_Word (shdr->sh_size, fp);
447 fputElf32_Word (shdr->sh_link, fp);
448 fputElf32_Word (shdr->sh_info, fp);
449 fputElf32_Word (shdr->sh_addralign, fp);
450 fputElf32_Word (shdr->sh_entsize, fp);
453 /*-------------------------------------------------------------------------*/
454 /* fputElf32_Ehdr - writes an Elf32_Phdr struct (segment header) to a file */
455 /*-------------------------------------------------------------------------*/
457 fputElf32_Phdr (Elf32_Phdr * phdr, FILE * fp)
459 fputElf32_Word (phdr->p_type, fp);
460 fputElf32_Off (phdr->p_offset, fp);
461 fputElf32_Addr (phdr->p_vaddr, fp);
462 fputElf32_Addr (phdr->p_paddr, fp);
463 fputElf32_Word (phdr->p_filesz, fp);
464 fputElf32_Word (phdr->p_memsz, fp);
465 fputElf32_Word (phdr->p_flags, fp);
466 fputElf32_Word (phdr->p_align, fp);
470 /*--------------------------------------------------------------------------*/
471 /* elfGenerateAbs - generates segments and sections for an absolute area. */
472 /* This is a little more complicated than a relative area since it may */
473 /* contain noncontiguous regions. */
474 /*--------------------------------------------------------------------------*/
476 elfGenerateAbs (struct area *ap, listHeader * segments, listHeader * sections)
492 /* Find the start of a contiguously */
493 /* used region within this area */
494 while (ofs < ap->a_size && !ap->a_used[ofs])
496 if (ofs >= ap->a_size)
499 /* Find the end of the region */
500 addr = ap->a_addr + ofs;
501 while (ofs < ap->a_size && ap->a_used[ofs])
503 size = ap->a_addr + ofs - addr;
505 /* create a segment header for this region if loadable */
506 if (!(ap->a_flag & A_NOLOAD))
508 phdrp = new (sizeof (*phdrp));
509 phdrp->p_type = PT_LOAD;
510 phdrp->p_offset = ftell (ofp);
511 phdrp->p_vaddr = addr;
512 phdrp->p_paddr = addr;
513 phdrp->p_filesz = size;
514 phdrp->p_memsz = size;
515 phdrp->p_flags = PF_R;
516 if (ap->a_flag & A_CODE)
517 phdrp->p_flags |= PF_X;
519 listAdd (segments, phdrp);
522 /* create a section header for this region */
523 shdrp = new (sizeof (*shdrp));
524 shdrp->sh_name = strtabFindOrAdd (&shstrtab, ap->a_id);
525 shdrp->sh_type = SHT_PROGBITS;
527 if (!(ap->a_flag & A_NOLOAD))
528 shdrp->sh_flags |= SHF_ALLOC;
529 if (ap->a_flag & A_CODE)
530 shdrp->sh_flags |= SHF_EXECINSTR;
531 shdrp->sh_addr = addr;
532 shdrp->sh_offset = ftell (ofp);
533 shdrp->sh_size = size;
536 shdrp->sh_addralign = 0;
537 shdrp->sh_entsize = 0;
538 listAdd (sections, shdrp);
540 fwrite (&ap->a_image[addr-ap->a_addr], 1, size, ofp);
544 /*--------------------------------------------------------------------------*/
545 /* elfGenerateRel - generates a segment and section for a relative area. */
546 /*--------------------------------------------------------------------------*/
548 elfGenerateRel (struct area *ap, listHeader * segments, listHeader * sections)
558 /* create a segment header for this area if loadable */
559 if (!(ap->a_flag & A_NOLOAD))
561 phdrp = new (sizeof (*phdrp));
562 phdrp->p_type = PT_LOAD;
563 phdrp->p_offset = ftell (ofp);
564 phdrp->p_vaddr = ap->a_addr;
565 phdrp->p_paddr = ap->a_addr;
566 phdrp->p_filesz = ap->a_size;
567 phdrp->p_memsz = ap->a_size;
568 phdrp->p_flags = PF_R;
569 if (ap->a_flag & A_CODE)
570 phdrp->p_flags |= PF_X;
572 listAdd (segments, phdrp);
575 /* create a section header for this area */
576 shdrp = new (sizeof (*shdrp));
577 shdrp->sh_name = strtabFindOrAdd (&shstrtab, ap->a_id);
578 shdrp->sh_type = SHT_PROGBITS;
580 if (!(ap->a_flag & A_NOLOAD))
581 shdrp->sh_flags |= SHF_ALLOC;
582 if (ap->a_flag & A_CODE)
583 shdrp->sh_flags |= SHF_EXECINSTR;
584 shdrp->sh_addr = ap->a_addr;
585 shdrp->sh_offset = ftell (ofp);
586 shdrp->sh_size = ap->a_size;
589 shdrp->sh_addralign = 0;
590 shdrp->sh_entsize = 0;
591 listAdd (sections, shdrp);
593 fwrite (ap->a_image, 1, ap->a_size, ofp);
596 /*--------------------------------------------------------------------------*/
597 /* elfGenerate - generates the complete ELF file */
598 /*--------------------------------------------------------------------------*/
602 listHeader * sections = listNew();
603 listHeader * segments = listNew();
610 Elf32_Word shstrtabName;
612 /* create the null section header for index 0 */
613 shdrp = new (sizeof (*shdrp));
615 shdrp->sh_type = SHT_NULL;
618 shdrp->sh_offset = 0;
622 shdrp->sh_addralign = 0;
623 shdrp->sh_entsize = 0;
624 listAdd (sections, shdrp);
626 /* Initialize the ELF header */
627 for (i=0; i<EI_NIDENT; i++)
629 ehdr.e_ident[EI_MAG0] = ELFMAG0;
630 ehdr.e_ident[EI_MAG1] = ELFMAG1;
631 ehdr.e_ident[EI_MAG2] = ELFMAG2;
632 ehdr.e_ident[EI_MAG3] = ELFMAG3;
633 ehdr.e_ident[EI_CLASS] = ELFCLASS32;
635 ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
637 ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
638 ehdr.e_ident[EI_VERSION] = 1;
639 ehdr.e_type = ET_EXEC;
640 ehdr.e_machine = EM_68HC08; /* FIXME: get rid of hardcoded value - EEP */
641 ehdr.e_phentsize = sizeof (*phdrp);
642 ehdr.e_shentsize = sizeof (*shdrp);
643 ehdr.e_ehsize = sizeof (ehdr);
649 if (execStartMSBfound && execStartLSBfound)
650 ehdr.e_entry = (execStartMSB << 8) + execStartLSB;
652 /* Write out the ELF header as a placeholder; we will update */
653 /* it with the final values when everything is complete */
654 fputElf32_Ehdr (&ehdr, ofp);
656 /* Iterate over the linker areas to generate */
657 /* the ELF sections and segments */
663 if (ap->a_flag & A_ABS)
664 elfGenerateAbs (ap, segments, sections);
666 elfGenerateRel (ap, segments, sections);
671 /* Create the string table section after the other sections */
672 shdrp = new (sizeof (*shdrp));
673 shdrp->sh_name = strtabFindOrAdd (&shstrtab, ".shstrtab");
674 shdrp->sh_type = SHT_STRTAB;
677 shdrp->sh_offset = ftell (ofp);
678 shdrp->sh_size = shstrtab.last->index + strlen (shstrtab.last->string) + 1;
681 shdrp->sh_addralign = 0;
682 shdrp->sh_entsize = 0;
683 listAdd (sections, shdrp);
684 fputElfStrtab (&shstrtab, ofp);
686 /* Find the index of the section string table */
687 /* header and save it in the ELF header */
689 shstrtabName = shdrp->sh_name;
690 lep = sections->first;
694 if (shdrp->sh_name == shstrtabName)
700 /* Write out the segment headers */
701 ehdr.e_phnum = segments->count;
702 ehdr.e_phoff = ftell (ofp);
703 lep = segments->first;
707 fputElf32_Phdr (phdrp, ofp);
711 /* Write out the section headers */
712 ehdr.e_shnum = sections->count;
713 ehdr.e_shoff = ftell (ofp);
714 lep = sections->first;
718 fputElf32_Shdr (shdrp, ofp);
722 /* All the values in the ELF header have now been computed; write */
723 /* over the placeholder header with the final values */
724 fseek (ofp, 0, SEEK_SET);
725 fputElf32_Ehdr (&ehdr, ofp);
726 fseek (ofp, 0, SEEK_END);
729 /*--------------------------------------------------------------------------*/
730 /* elf - incrementally called by the linker core to generate ELF file data. */
731 /* The parameter is nonzero when there is data available and zero when */
732 /* the linker is finished. */
733 /*--------------------------------------------------------------------------*/
739 /* Buffer the data until we have it all */
743 address = rtval[0] + (rtval[1] << 8); /* little endian order */
745 address = rtval[1] + (rtval[0] << 8); /* big endian order */
747 /* If this area doesn't have an image buffer, create one */
750 ap->a_image = new (ap->a_size);
751 if (ap->a_flag & A_ABS)
752 ap->a_used = new (ap->a_size);
755 /* Copy the data into the image buffer */
756 for (i = 2; i < rtcnt ; i++)
760 ap->a_image[address-ap->a_addr] = rtval[i];
762 ap->a_used[address-ap->a_addr] = 1;
764 /* Make note of the reset vector */
765 if (!(ap->a_flag & A_NOLOAD))
767 if (address == 0xfffe)
769 execStartMSB = rtval[i];
770 execStartMSBfound = 1;
772 if (address == 0xffff)
774 execStartLSB = rtval[i];
775 execStartLSBfound = 1;