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 -------------------------------------------------------------------------*/
27 static int execStartMSB;
28 static int execStartLSB;
29 static char execStartMSBfound;
30 static char execStartLSBfound;
32 typedef TYPE_UDWORD Elf32_Addr;
33 typedef TYPE_WORD Elf32_Half;
34 typedef TYPE_UDWORD Elf32_Off;
35 typedef TYPE_DWORD Elf32_Sword;
36 typedef TYPE_UDWORD Elf32_Word;
68 /* These e_machine values are from "Motorola 8- and 16-bit Embedded */
69 /* Application Binary Interface (M8/16EABI)" version 2.0 */
118 SHF_WRITE = (1 << 0),
119 SHF_ALLOC = (1 << 1),
120 SHF_EXECINSTR = (1 << 2),
138 unsigned char e_ident[EI_NIDENT];
140 Elf32_Half e_machine;
141 Elf32_Word e_version;
147 Elf32_Half e_phentsize;
149 Elf32_Half e_shentsize;
151 Elf32_Half e_shstrndx;
164 Elf32_Word sh_addralign;
165 Elf32_Word sh_entsize;
180 typedef struct strtabString
183 struct strtabString * prev;
184 struct strtabString * next;
190 strtabString * first;
194 static strtabList shstrtab;
197 typedef struct listEntry
200 struct listEntry * prev;
201 struct listEntry * next;
214 listAdd (listHeader * lhp, void * item)
218 lep = new (sizeof (*lep));
220 lep->prev = lhp->last;
222 lep->prev->next = lep;
236 lhp = new (sizeof (*lhp));
244 strtabFind (strtabList * strtab, char * str)
251 if (!strcmp (str, sp->string))
260 /*-------------------------------------------------------------------*/
261 /* strtabFindOrAdd - Finds a string in a string table or adds the */
262 /* string if it does not already exist. Returns the offset of the */
263 /* string in the table. */
264 /*-------------------------------------------------------------------*/
266 strtabFindOrAdd (strtabList * strtab, char * str)
273 if (!strcmp (str, sp->string))
278 sp = new (sizeof(*sp));
280 sp->index = strtab->last->index + 1 + strlen (strtab->last->string);
283 sp->string = new (1+strlen (str));
284 strcpy (sp->string, str);
286 sp->prev = strtab->last;
296 /*-------------------------------------------------------------------*/
297 /* fputElfStrtab - writes a string table to a file */
298 /*-------------------------------------------------------------------*/
300 fputElfStrtab (strtabList *strtab, FILE *fp)
304 fputc (0, fp); /* index 0 must be the null character */
309 fputs (sp->string, fp);
315 /*-------------------------------------------------------------------*/
316 /* fputElf32_Word - writes an Elf32_Word value to a file */
317 /*-------------------------------------------------------------------*/
319 fputElf32_Word (Elf32_Word x, FILE *fp)
323 fputc (x & 0xff, fp);
324 fputc ((x >> 8) & 0xff, fp);
325 fputc ((x >> 16) & 0xff, fp);
326 fputc ((x >> 24) & 0xff, fp);
330 fputc ((x >> 24) & 0xff, fp);
331 fputc ((x >> 16) & 0xff, fp);
332 fputc ((x >> 8) & 0xff, fp);
333 fputc (x & 0xff, fp);
337 /*-------------------------------------------------------------------*/
338 /* fputElf32_Off - writes an Elf32_Off value to a file */
339 /*-------------------------------------------------------------------*/
341 fputElf32_Off (Elf32_Off x, FILE *fp)
345 fputc (x & 0xff, fp);
346 fputc ((x >> 8) & 0xff, fp);
347 fputc ((x >> 16) & 0xff, fp);
348 fputc ((x >> 24) & 0xff, fp);
352 fputc ((x >> 24) & 0xff, fp);
353 fputc ((x >> 16) & 0xff, fp);
354 fputc ((x >> 8) & 0xff, fp);
355 fputc (x & 0xff, fp);
359 /*-------------------------------------------------------------------*/
360 /* fputElf32_Addr - writes an Elf32_Addr value to a file */
361 /*-------------------------------------------------------------------*/
363 fputElf32_Addr (Elf32_Addr x, FILE *fp)
367 fputc (x & 0xff, fp);
368 fputc ((x >> 8) & 0xff, fp);
369 fputc ((x >> 16) & 0xff, fp);
370 fputc ((x >> 24) & 0xff, fp);
374 fputc ((x >> 24) & 0xff, fp);
375 fputc ((x >> 16) & 0xff, fp);
376 fputc ((x >> 8) & 0xff, fp);
377 fputc (x & 0xff, fp);
381 /*-------------------------------------------------------------------*/
382 /* fputElf32_Half - writes an Elf32_Half value to a file */
383 /*-------------------------------------------------------------------*/
385 fputElf32_Half (Elf32_Half x, FILE *fp)
389 fputc (x & 0xff, fp);
390 fputc ((x >> 8) & 0xff, fp);
394 fputc ((x >> 8) & 0xff, fp);
395 fputc (x & 0xff, fp);
399 /*------------------------------------------------------------------------*/
400 /* fputElf32_Ehdr - writes an Elf32_Ehdr struct (ELF header) to a file */
401 /*------------------------------------------------------------------------*/
403 fputElf32_Ehdr (Elf32_Ehdr * ehdr, FILE * fp)
407 for (i=0; i<EI_NIDENT; i++)
408 fputc (ehdr->e_ident[i], fp);
410 fputElf32_Half (ehdr->e_type, fp);
411 fputElf32_Half (ehdr->e_machine, fp);
412 fputElf32_Word (ehdr->e_version, fp);
413 fputElf32_Addr (ehdr->e_entry, fp);
414 fputElf32_Off (ehdr->e_phoff, fp);
415 fputElf32_Off (ehdr->e_shoff, fp);
416 fputElf32_Word (ehdr->e_flags, fp);
417 fputElf32_Half (ehdr->e_ehsize, fp);
418 fputElf32_Half (ehdr->e_phentsize, fp);
419 fputElf32_Half (ehdr->e_phnum, fp);
420 fputElf32_Half (ehdr->e_shentsize, fp);
421 fputElf32_Half (ehdr->e_shnum, fp);
422 fputElf32_Half (ehdr->e_shstrndx, fp);
425 /*-------------------------------------------------------------------------*/
426 /* fputElf32_Ehdr - writes an Elf32_Shdr struct (section header) to a file */
427 /*-------------------------------------------------------------------------*/
429 fputElf32_Shdr (Elf32_Shdr * shdr, FILE * fp)
431 fputElf32_Word (shdr->sh_name, fp);
432 fputElf32_Word (shdr->sh_type, fp);
433 fputElf32_Word (shdr->sh_flags, fp);
434 fputElf32_Addr (shdr->sh_addr, fp);
435 fputElf32_Off (shdr->sh_offset, fp);
436 fputElf32_Word (shdr->sh_size, fp);
437 fputElf32_Word (shdr->sh_link, fp);
438 fputElf32_Word (shdr->sh_info, fp);
439 fputElf32_Word (shdr->sh_addralign, fp);
440 fputElf32_Word (shdr->sh_entsize, fp);
443 /*-------------------------------------------------------------------------*/
444 /* fputElf32_Ehdr - writes an Elf32_Phdr struct (segment header) to a file */
445 /*-------------------------------------------------------------------------*/
447 fputElf32_Phdr (Elf32_Phdr * phdr, FILE * fp)
449 fputElf32_Word (phdr->p_type, fp);
450 fputElf32_Off (phdr->p_offset, fp);
451 fputElf32_Addr (phdr->p_vaddr, fp);
452 fputElf32_Addr (phdr->p_paddr, fp);
453 fputElf32_Word (phdr->p_filesz, fp);
454 fputElf32_Word (phdr->p_memsz, fp);
455 fputElf32_Word (phdr->p_flags, fp);
456 fputElf32_Word (phdr->p_align, fp);
460 /*--------------------------------------------------------------------------*/
461 /* elfGenerateAbs - generates segments and sections for an absolute area. */
462 /* This is a little more complicated than a relative area since it may */
463 /* contain noncontiguous regions. */
464 /*--------------------------------------------------------------------------*/
466 elfGenerateAbs (struct area *ap, listHeader * segments, listHeader * sections)
482 /* Find the start of a contiguously */
483 /* used region within this area */
484 while (ofs < ap->a_size && !ap->a_used[ofs])
486 if (ofs >= ap->a_size)
489 /* Find the end of the region */
490 addr = ap->a_addr + ofs;
491 while (ofs < ap->a_size && ap->a_used[ofs])
493 size = ap->a_addr + ofs - addr;
495 /* create a segment header for this region if loadable */
496 if (!(ap->a_flag & A_NOLOAD))
498 phdrp = new (sizeof (*phdrp));
499 phdrp->p_type = PT_LOAD;
500 phdrp->p_offset = ftell (ofp);
501 phdrp->p_vaddr = addr;
502 phdrp->p_paddr = addr;
503 phdrp->p_filesz = size;
504 phdrp->p_memsz = size;
505 phdrp->p_flags = PF_R;
506 if (ap->a_flag & A_CODE)
507 phdrp->p_flags |= PF_X;
509 listAdd (segments, phdrp);
512 /* create a section header for this region */
513 shdrp = new (sizeof (*shdrp));
514 shdrp->sh_name = strtabFindOrAdd (&shstrtab, ap->a_id);
515 shdrp->sh_type = SHT_PROGBITS;
517 if (!(ap->a_flag & A_NOLOAD))
518 shdrp->sh_flags |= SHF_ALLOC;
519 if (ap->a_flag & A_CODE)
520 shdrp->sh_flags |= SHF_EXECINSTR;
521 shdrp->sh_addr = addr;
522 shdrp->sh_offset = ftell (ofp);
523 shdrp->sh_size = size;
526 shdrp->sh_addralign = 0;
527 shdrp->sh_entsize = 0;
528 listAdd (sections, shdrp);
530 fwrite (&ap->a_image[addr-ap->a_addr], 1, size, ofp);
534 /*--------------------------------------------------------------------------*/
535 /* elfGenerateRel - generates a segment and section for a relative area. */
536 /*--------------------------------------------------------------------------*/
538 elfGenerateRel (struct area *ap, listHeader * segments, listHeader * sections)
548 /* create a segment header for this area if loadable */
549 if (!(ap->a_flag & A_NOLOAD))
551 phdrp = new (sizeof (*phdrp));
552 phdrp->p_type = PT_LOAD;
553 phdrp->p_offset = ftell (ofp);
554 phdrp->p_vaddr = ap->a_addr;
555 phdrp->p_paddr = ap->a_addr;
556 phdrp->p_filesz = ap->a_size;
557 phdrp->p_memsz = ap->a_size;
558 phdrp->p_flags = PF_R;
559 if (ap->a_flag & A_CODE)
560 phdrp->p_flags |= PF_X;
562 listAdd (segments, phdrp);
565 /* create a section header for this area */
566 shdrp = new (sizeof (*shdrp));
567 shdrp->sh_name = strtabFindOrAdd (&shstrtab, ap->a_id);
568 shdrp->sh_type = SHT_PROGBITS;
570 if (!(ap->a_flag & A_NOLOAD))
571 shdrp->sh_flags |= SHF_ALLOC;
572 if (ap->a_flag & A_CODE)
573 shdrp->sh_flags |= SHF_EXECINSTR;
574 shdrp->sh_addr = ap->a_addr;
575 shdrp->sh_offset = ftell (ofp);
576 shdrp->sh_size = ap->a_size;
579 shdrp->sh_addralign = 0;
580 shdrp->sh_entsize = 0;
581 listAdd (sections, shdrp);
583 fwrite (ap->a_image, 1, ap->a_size, ofp);
586 /*--------------------------------------------------------------------------*/
587 /* elfGenerate - generates the complete ELF file */
588 /*--------------------------------------------------------------------------*/
592 listHeader * sections = listNew();
593 listHeader * segments = listNew();
600 Elf32_Word shstrtabName;
602 /* create the null section header for index 0 */
603 shdrp = new (sizeof (*shdrp));
605 shdrp->sh_type = SHT_NULL;
608 shdrp->sh_offset = 0;
612 shdrp->sh_addralign = 0;
613 shdrp->sh_entsize = 0;
614 listAdd (sections, shdrp);
616 /* Initialize the ELF header */
617 for (i=0; i<EI_NIDENT; i++)
619 ehdr.e_ident[EI_MAG0] = ELFMAG0;
620 ehdr.e_ident[EI_MAG1] = ELFMAG1;
621 ehdr.e_ident[EI_MAG2] = ELFMAG2;
622 ehdr.e_ident[EI_MAG3] = ELFMAG3;
623 ehdr.e_ident[EI_CLASS] = ELFCLASS32;
625 ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
627 ehdr.e_ident[EI_DATA] = ELFDATA2MSB;
628 ehdr.e_ident[EI_VERSION] = 1;
629 ehdr.e_type = ET_EXEC;
630 ehdr.e_machine = EM_68HC08; /* FIXME: get rid of hardcoded value - EEP */
631 ehdr.e_phentsize = sizeof (*phdrp);
632 ehdr.e_shentsize = sizeof (*shdrp);
633 ehdr.e_ehsize = sizeof (ehdr);
639 if (execStartMSBfound && execStartLSBfound)
640 ehdr.e_entry = (execStartMSB << 8) + execStartLSB;
642 /* Write out the ELF header as a placeholder; we will update */
643 /* it with the final values when everything is complete */
644 fputElf32_Ehdr (&ehdr, ofp);
646 /* Iterate over the linker areas to generate */
647 /* the ELF sections and segments */
653 if (ap->a_flag & A_ABS)
654 elfGenerateAbs (ap, segments, sections);
656 elfGenerateRel (ap, segments, sections);
661 /* Create the string table section after the other sections */
662 shdrp = new (sizeof (*shdrp));
663 shdrp->sh_name = strtabFindOrAdd (&shstrtab, ".shstrtab");
664 shdrp->sh_type = SHT_STRTAB;
667 shdrp->sh_offset = ftell (ofp);
668 shdrp->sh_size = shstrtab.last->index + strlen (shstrtab.last->string) + 1;
671 shdrp->sh_addralign = 0;
672 shdrp->sh_entsize = 0;
673 listAdd (sections, shdrp);
674 fputElfStrtab (&shstrtab, ofp);
676 /* Find the index of the section string table */
677 /* header and save it in the ELF header */
679 shstrtabName = shdrp->sh_name;
680 lep = sections->first;
684 if (shdrp->sh_name == shstrtabName)
690 /* Write out the segment headers */
691 ehdr.e_phnum = segments->count;
692 ehdr.e_phoff = ftell (ofp);
693 lep = segments->first;
697 fputElf32_Phdr (phdrp, ofp);
701 /* Write out the section headers */
702 ehdr.e_shnum = sections->count;
703 ehdr.e_shoff = ftell (ofp);
704 lep = sections->first;
708 fputElf32_Shdr (shdrp, ofp);
712 /* All the values in the ELF header have now been computed; write */
713 /* over the placeholder header with the final values */
714 fseek (ofp, 0, SEEK_SET);
715 fputElf32_Ehdr (&ehdr, ofp);
716 fseek (ofp, 0, SEEK_END);
719 /*--------------------------------------------------------------------------*/
720 /* elf - incrementally called by the linker core to generate ELF file data. */
721 /* The parameter is nonzero when there is data available and zero when */
722 /* the linker is finished. */
723 /*--------------------------------------------------------------------------*/
729 /* Buffer the data until we have it all */
733 address = rtval[0] + (rtval[1] << 8); /* little endian order */
735 address = rtval[1] + (rtval[0] << 8); /* big endian order */
737 /* If this area doesn't have an image buffer, create one */
740 ap->a_image = new (ap->a_size);
741 if (ap->a_flag & A_ABS)
742 ap->a_used = new (ap->a_size);
745 /* Copy the data into the image buffer */
746 for (i = 2; i < rtcnt ; i++)
750 ap->a_image[address-ap->a_addr] = rtval[i];
752 ap->a_used[address-ap->a_addr] = 1;
754 /* Make note of the reset vector */
755 if (!(ap->a_flag & A_NOLOAD))
757 if (address == 0xfffe)
759 execStartMSB = rtval[i];
760 execStartMSBfound = 1;
762 if (address == 0xffff)
764 execStartLSB = rtval[i];
765 execStartLSBfound = 1;