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 static int execStartMSB;
29 static int execStartLSB;
30 static char execStartMSBfound;
31 static char execStartLSBfound;
33 typedef TYPE_UDWORD Elf32_Addr;
34 typedef TYPE_WORD Elf32_Half;
35 typedef TYPE_UDWORD Elf32_Off;
36 typedef TYPE_DWORD Elf32_Sword;
37 typedef TYPE_UDWORD Elf32_Word;
69 /* These e_machine values are from "Motorola 8- and 16-bit Embedded */
70 /* Application Binary Interface (M8/16EABI)" version 2.0 */
119 SHF_WRITE = (1 << 0),
120 SHF_ALLOC = (1 << 1),
121 SHF_EXECINSTR = (1 << 2),
139 unsigned char e_ident[EI_NIDENT];
141 Elf32_Half e_machine;
142 Elf32_Word e_version;
148 Elf32_Half e_phentsize;
150 Elf32_Half e_shentsize;
152 Elf32_Half e_shstrndx;
165 Elf32_Word sh_addralign;
166 Elf32_Word sh_entsize;
181 typedef struct strtabString
184 struct strtabString * prev;
185 struct strtabString * next;
191 strtabString * first;
195 static strtabList shstrtab;
198 typedef struct listEntry
201 struct listEntry * prev;
202 struct listEntry * next;
215 listAdd (listHeader * lhp, void * item)
219 lep = new (sizeof (*lep));
221 lep->prev = lhp->last;
223 lep->prev->next = lep;
237 lhp = new (sizeof (*lhp));
245 strtabFind (strtabList * strtab, char * str)
252 if (!strcmp (str, sp->string))
261 /*-------------------------------------------------------------------*/
262 /* strtabFindOrAdd - Finds a string in a string table or adds the */
263 /* string if it does not already exist. Returns the offset of the */
264 /* string in the table. */
265 /*-------------------------------------------------------------------*/
267 strtabFindOrAdd (strtabList * strtab, char * str)
274 if (!strcmp (str, sp->string))
279 sp = new (sizeof(*sp));
281 sp->index = strtab->last->index + 1 + strlen (strtab->last->string);
284 sp->string = new (1+strlen (str));
285 strcpy (sp->string, str);
287 sp->prev = strtab->last;
297 /*-------------------------------------------------------------------*/
298 /* fputElfStrtab - writes a string table to a file */
299 /*-------------------------------------------------------------------*/
301 fputElfStrtab (strtabList *strtab, FILE *fp)
305 fputc (0, fp); /* index 0 must be the null character */
310 fputs (sp->string, fp);
316 /*-------------------------------------------------------------------*/
317 /* fputElf32_Word - writes an Elf32_Word value to a file */
318 /*-------------------------------------------------------------------*/
320 fputElf32_Word (Elf32_Word x, FILE *fp)
324 fputc (x & 0xff, fp);
325 fputc ((x >> 8) & 0xff, fp);
326 fputc ((x >> 16) & 0xff, fp);
327 fputc ((x >> 24) & 0xff, fp);
331 fputc ((x >> 24) & 0xff, fp);
332 fputc ((x >> 16) & 0xff, fp);
333 fputc ((x >> 8) & 0xff, fp);
334 fputc (x & 0xff, fp);
338 /*-------------------------------------------------------------------*/
339 /* fputElf32_Off - writes an Elf32_Off value to a file */
340 /*-------------------------------------------------------------------*/
342 fputElf32_Off (Elf32_Off x, FILE *fp)
346 fputc (x & 0xff, fp);
347 fputc ((x >> 8) & 0xff, fp);
348 fputc ((x >> 16) & 0xff, fp);
349 fputc ((x >> 24) & 0xff, fp);
353 fputc ((x >> 24) & 0xff, fp);
354 fputc ((x >> 16) & 0xff, fp);
355 fputc ((x >> 8) & 0xff, fp);
356 fputc (x & 0xff, fp);
360 /*-------------------------------------------------------------------*/
361 /* fputElf32_Addr - writes an Elf32_Addr value to a file */
362 /*-------------------------------------------------------------------*/
364 fputElf32_Addr (Elf32_Addr x, FILE *fp)
368 fputc (x & 0xff, fp);
369 fputc ((x >> 8) & 0xff, fp);
370 fputc ((x >> 16) & 0xff, fp);
371 fputc ((x >> 24) & 0xff, fp);
375 fputc ((x >> 24) & 0xff, fp);
376 fputc ((x >> 16) & 0xff, fp);
377 fputc ((x >> 8) & 0xff, fp);
378 fputc (x & 0xff, fp);
382 /*-------------------------------------------------------------------*/
383 /* fputElf32_Half - writes an Elf32_Half value to a file */
384 /*-------------------------------------------------------------------*/
386 fputElf32_Half (Elf32_Half x, FILE *fp)
390 fputc (x & 0xff, fp);
391 fputc ((x >> 8) & 0xff, fp);
395 fputc ((x >> 8) & 0xff, fp);
396 fputc (x & 0xff, fp);
400 /*------------------------------------------------------------------------*/
401 /* fputElf32_Ehdr - writes an Elf32_Ehdr struct (ELF header) to a file */
402 /*------------------------------------------------------------------------*/
404 fputElf32_Ehdr (Elf32_Ehdr * ehdr, FILE * fp)
408 for (i=0; i<EI_NIDENT; i++)
409 fputc (ehdr->e_ident[i], fp);
411 fputElf32_Half (ehdr->e_type, fp);
412 fputElf32_Half (ehdr->e_machine, fp);
413 fputElf32_Word (ehdr->e_version, fp);
414 fputElf32_Addr (ehdr->e_entry, fp);
415 fputElf32_Off (ehdr->e_phoff, fp);
416 fputElf32_Off (ehdr->e_shoff, fp);
417 fputElf32_Word (ehdr->e_flags, fp);
418 fputElf32_Half (ehdr->e_ehsize, fp);
419 fputElf32_Half (ehdr->e_phentsize, fp);
420 fputElf32_Half (ehdr->e_phnum, fp);
421 fputElf32_Half (ehdr->e_shentsize, fp);
422 fputElf32_Half (ehdr->e_shnum, fp);
423 fputElf32_Half (ehdr->e_shstrndx, fp);
426 /*-------------------------------------------------------------------------*/
427 /* fputElf32_Ehdr - writes an Elf32_Shdr struct (section header) to a file */
428 /*-------------------------------------------------------------------------*/
430 fputElf32_Shdr (Elf32_Shdr * shdr, FILE * fp)
432 fputElf32_Word (shdr->sh_name, fp);
433 fputElf32_Word (shdr->sh_type, fp);
434 fputElf32_Word (shdr->sh_flags, fp);
435 fputElf32_Addr (shdr->sh_addr, fp);
436 fputElf32_Off (shdr->sh_offset, fp);
437 fputElf32_Word (shdr->sh_size, fp);
438 fputElf32_Word (shdr->sh_link, fp);
439 fputElf32_Word (shdr->sh_info, fp);
440 fputElf32_Word (shdr->sh_addralign, fp);
441 fputElf32_Word (shdr->sh_entsize, fp);
444 /*-------------------------------------------------------------------------*/
445 /* fputElf32_Ehdr - writes an Elf32_Phdr struct (segment header) to a file */
446 /*-------------------------------------------------------------------------*/
448 fputElf32_Phdr (Elf32_Phdr * phdr, FILE * fp)
450 fputElf32_Word (phdr->p_type, fp);
451 fputElf32_Off (phdr->p_offset, fp);
452 fputElf32_Addr (phdr->p_vaddr, fp);
453 fputElf32_Addr (phdr->p_paddr, fp);
454 fputElf32_Word (phdr->p_filesz, fp);
455 fputElf32_Word (phdr->p_memsz, fp);
456 fputElf32_Word (phdr->p_flags, fp);
457 fputElf32_Word (phdr->p_align, fp);
461 /*--------------------------------------------------------------------------*/
462 /* elfGenerateAbs - generates segments and sections for an absolute area. */
463 /* This is a little more complicated than a relative area since it may */
464 /* contain noncontiguous regions. */
465 /*--------------------------------------------------------------------------*/
467 elfGenerateAbs (struct area *ap, listHeader * segments, listHeader * sections)
483 /* Find the start of a contiguously */
484 /* used region within this area */
485 while (ofs < ap->a_size && !ap->a_used[ofs])
487 if (ofs >= ap->a_size)
490 /* Find the end of the region */
491 addr = ap->a_addr + ofs;
492 while (ofs < ap->a_size && ap->a_used[ofs])
494 size = ap->a_addr + ofs - addr;
496 /* create a segment header for this region if loadable */
497 if (!(ap->a_flag & A_NOLOAD))
499 phdrp = new (sizeof (*phdrp));
500 phdrp->p_type = PT_LOAD;
501 phdrp->p_offset = ftell (ofp);
502 phdrp->p_vaddr = addr;
503 phdrp->p_paddr = addr;
504 phdrp->p_filesz = size;
505 phdrp->p_memsz = size;
506 phdrp->p_flags = PF_R;
507 if (ap->a_flag & A_CODE)
508 phdrp->p_flags |= PF_X;
510 listAdd (segments, phdrp);
513 /* create a section header for this region */
514 shdrp = new (sizeof (*shdrp));
515 shdrp->sh_name = strtabFindOrAdd (&shstrtab, ap->a_id);
516 shdrp->sh_type = SHT_PROGBITS;
518 if (!(ap->a_flag & A_NOLOAD))
519 shdrp->sh_flags |= SHF_ALLOC;
520 if (ap->a_flag & A_CODE)
521 shdrp->sh_flags |= SHF_EXECINSTR;
522 shdrp->sh_addr = addr;
523 shdrp->sh_offset = ftell (ofp);
524 shdrp->sh_size = size;
527 shdrp->sh_addralign = 0;
528 shdrp->sh_entsize = 0;
529 listAdd (sections, shdrp);
531 fwrite (&ap->a_image[addr-ap->a_addr], 1, size, ofp);
535 /*--------------------------------------------------------------------------*/
536 /* elfGenerateRel - generates a segment and section for a relative area. */
537 /*--------------------------------------------------------------------------*/
539 elfGenerateRel (struct area *ap, listHeader * segments, listHeader * sections)
549 /* create a segment header for this area if loadable */
550 if (!(ap->a_flag & A_NOLOAD))
552 phdrp = new (sizeof (*phdrp));
553 phdrp->p_type = PT_LOAD;
554 phdrp->p_offset = ftell (ofp);
555 phdrp->p_vaddr = ap->a_addr;
556 phdrp->p_paddr = ap->a_addr;
557 phdrp->p_filesz = ap->a_size;
558 phdrp->p_memsz = ap->a_size;
559 phdrp->p_flags = PF_R;
560 if (ap->a_flag & A_CODE)
561 phdrp->p_flags |= PF_X;
563 listAdd (segments, phdrp);
566 /* create a section header for this area */
567 shdrp = new (sizeof (*shdrp));
568 shdrp->sh_name = strtabFindOrAdd (&shstrtab, ap->a_id);
569 shdrp->sh_type = SHT_PROGBITS;
571 if (!(ap->a_flag & A_NOLOAD))
572 shdrp->sh_flags |= SHF_ALLOC;
573 if (ap->a_flag & A_CODE)
574 shdrp->sh_flags |= SHF_EXECINSTR;
575 shdrp->sh_addr = ap->a_addr;
576 shdrp->sh_offset = ftell (ofp);
577 shdrp->sh_size = ap->a_size;
580 shdrp->sh_addralign = 0;
581 shdrp->sh_entsize = 0;
582 listAdd (sections, shdrp);
584 fwrite (ap->a_image, 1, ap->a_size, ofp);
587 /*--------------------------------------------------------------------------*/
588 /* elfGenerate - generates the complete ELF file */
589 /*--------------------------------------------------------------------------*/
593 listHeader * sections = listNew();
594 listHeader * segments = listNew();
601 Elf32_Word shstrtabName;
603 /* create the null section header for index 0 */
604 shdrp = new (sizeof (*shdrp));
606 shdrp->sh_type = SHT_NULL;
609 shdrp->sh_offset = 0;
613 shdrp->sh_addralign = 0;
614 shdrp->sh_entsize = 0;
615 listAdd (sections, shdrp);
617 /* Initialize the ELF header */
618 for (i=0; i<EI_NIDENT; i++)
620 ehdr.e_ident[EI_MAG0] = ELFMAG0;
621 ehdr.e_ident[EI_MAG1] = ELFMAG1;
622 ehdr.e_ident[EI_MAG2] = ELFMAG2;
623 ehdr.e_ident[EI_MAG3] = ELFMAG3;
624 ehdr.e_ident[EI_CLASS] = ELFCLASS32;
626 ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
628 ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
629 ehdr.e_ident[EI_VERSION] = 1;
630 ehdr.e_type = ET_EXEC;
631 ehdr.e_machine = EM_68HC08; /* FIXME: get rid of hardcoded value - EEP */
632 ehdr.e_phentsize = sizeof (*phdrp);
633 ehdr.e_shentsize = sizeof (*shdrp);
634 ehdr.e_ehsize = sizeof (ehdr);
640 if (execStartMSBfound && execStartLSBfound)
641 ehdr.e_entry = (execStartMSB << 8) + execStartLSB;
643 /* Write out the ELF header as a placeholder; we will update */
644 /* it with the final values when everything is complete */
645 fputElf32_Ehdr (&ehdr, ofp);
647 /* Iterate over the linker areas to generate */
648 /* the ELF sections and segments */
654 if (ap->a_flag & A_ABS)
655 elfGenerateAbs (ap, segments, sections);
657 elfGenerateRel (ap, segments, sections);
662 /* Create the string table section after the other sections */
663 shdrp = new (sizeof (*shdrp));
664 shdrp->sh_name = strtabFindOrAdd (&shstrtab, ".shstrtab");
665 shdrp->sh_type = SHT_STRTAB;
668 shdrp->sh_offset = ftell (ofp);
669 shdrp->sh_size = shstrtab.last->index + strlen (shstrtab.last->string) + 1;
672 shdrp->sh_addralign = 0;
673 shdrp->sh_entsize = 0;
674 listAdd (sections, shdrp);
675 fputElfStrtab (&shstrtab, ofp);
677 /* Find the index of the section string table */
678 /* header and save it in the ELF header */
680 shstrtabName = shdrp->sh_name;
681 lep = sections->first;
685 if (shdrp->sh_name == shstrtabName)
691 /* Write out the segment headers */
692 ehdr.e_phnum = segments->count;
693 ehdr.e_phoff = ftell (ofp);
694 lep = segments->first;
698 fputElf32_Phdr (phdrp, ofp);
702 /* Write out the section headers */
703 ehdr.e_shnum = sections->count;
704 ehdr.e_shoff = ftell (ofp);
705 lep = sections->first;
709 fputElf32_Shdr (shdrp, ofp);
713 /* All the values in the ELF header have now been computed; write */
714 /* over the placeholder header with the final values */
715 fseek (ofp, 0, SEEK_SET);
716 fputElf32_Ehdr (&ehdr, ofp);
717 fseek (ofp, 0, SEEK_END);
720 /*--------------------------------------------------------------------------*/
721 /* elf - incrementally called by the linker core to generate ELF file data. */
722 /* The parameter is nonzero when there is data available and zero when */
723 /* the linker is finished. */
724 /*--------------------------------------------------------------------------*/
730 /* Buffer the data until we have it all */
734 address = rtval[0] + (rtval[1] << 8); /* little endian order */
736 address = rtval[1] + (rtval[0] << 8); /* big endian order */
738 /* If this area doesn't have an image buffer, create one */
741 ap->a_image = new (ap->a_size);
742 if (ap->a_flag & A_ABS)
743 ap->a_used = new (ap->a_size);
746 /* Copy the data into the image buffer */
747 for (i = 2; i < rtcnt ; i++, address++)
751 ap->a_image[address-ap->a_addr] = rtval[i];
753 ap->a_used[address-ap->a_addr] = 1;
755 /* Make note of the reset vector */
756 if (!(ap->a_flag & A_NOLOAD))
758 if (address == 0xfffe)
760 execStartMSB = rtval[i];
761 execStartMSBfound = 1;
763 if (address == 0xffff)
765 execStartLSB = rtval[i];
766 execStartLSBfound = 1;