]> granicus.if.org Git - yasm/commitdiff
Add some ELF32 support functions. Note: currently only supports little-endian
authorPeter Johnson <peter@tortall.net>
Wed, 19 Mar 2003 04:56:06 +0000 (04:56 -0000)
committerPeter Johnson <peter@tortall.net>
Wed, 19 Mar 2003 04:56:06 +0000 (04:56 -0000)
ELF.

svn path=/trunk/yasm/; revision=881

modules/objfmts/elf/elf32.c [new file with mode: 0644]
modules/objfmts/elf/elf32.h

diff --git a/modules/objfmts/elf/elf32.c b/modules/objfmts/elf/elf32.c
new file mode 100644 (file)
index 0000000..4a830de
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * ELF-32 utility functions
+ *
+ *  Copyright (C) 2003  Peter Johnson
+ *
+ *  This file is part of YASM.
+ *
+ *  YASM is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  YASM is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#define YASM_LIB_INTERNAL
+#include "libyasm.h"
+/*@unused@*/ RCSID("$IdPath$");
+
+#include "elf32.h"
+
+/* Converts an ELF-32 header into little endian bytes */
+void
+elf32_write_elf_header(unsigned char **bufp, Elf32_Ehdr *ehdr)
+{
+    int i;
+
+    for (i=0; i<EI_NIDENT; i++)
+       YASM_WRITE_8(*bufp, ehdr->e_ident[i]);
+    YASM_WRITE_16_L(*bufp, ehdr->e_type);
+    YASM_WRITE_16_L(*bufp, ehdr->e_machine);
+    YASM_WRITE_32_L(*bufp, ehdr->e_version);
+    YASM_WRITE_32_L(*bufp, ehdr->e_entry);
+    YASM_WRITE_32_L(*bufp, ehdr->e_phoff);
+    YASM_WRITE_32_L(*bufp, ehdr->e_shoff);
+    YASM_WRITE_32_L(*bufp, ehdr->e_flags);
+    YASM_WRITE_16_L(*bufp, ehdr->e_ehsize);
+    YASM_WRITE_16_L(*bufp, ehdr->e_phentsize);
+    YASM_WRITE_16_L(*bufp, ehdr->e_phnum);
+    YASM_WRITE_16_L(*bufp, ehdr->e_shentsize);
+    YASM_WRITE_16_L(*bufp, ehdr->e_shnum);
+    YASM_WRITE_16_L(*bufp, ehdr->e_shstrndx);
+}
+
+/* Converts an ELF-32 section header into little endian bytes */
+void
+elf32_write_sect_header(unsigned char **bufp, Elf32_Shdr *shdr)
+{
+    YASM_WRITE_32_L(*bufp, shdr->sh_name);
+    YASM_WRITE_32_L(*bufp, shdr->sh_type);
+    YASM_WRITE_32_L(*bufp, shdr->sh_flags);
+    YASM_WRITE_32_L(*bufp, shdr->sh_addr);
+    YASM_WRITE_32_L(*bufp, shdr->sh_offset);
+    YASM_WRITE_32_L(*bufp, shdr->sh_size);
+    YASM_WRITE_32_L(*bufp, shdr->sh_link);
+    YASM_WRITE_32_L(*bufp, shdr->sh_info);
+    YASM_WRITE_32_L(*bufp, shdr->sh_addralign);
+    YASM_WRITE_32_L(*bufp, shdr->sh_entsize);
+}
+
+void
+elf32_print_sect_header(FILE *f, int indent_level, Elf32_Shdr *shdr)
+{
+    static const char *sh_type_names[] = {
+       "null", "progbits", "symtab", "strtab", "rela", "hash", "dynamic",
+       "note", "nobits", "rel", "shlib", "dynsym"
+    };
+
+    fprintf(f, "%*ssh_name=%lu\n", indent_level, "", shdr->sh_name);
+    fprintf(f, "%*ssh_type=", indent_level, "");
+    if (shdr->sh_type < NELEMS(sh_type_names))
+       fprintf(f, "%s", sh_type_names[shdr->sh_type]);
+    else
+       fprintf(f, "%lu", shdr->sh_type);
+    fprintf(f, "\n%*ssh_flags=%lx\n", indent_level, "", shdr->sh_flags);
+    fprintf(f, "%*ssh_addr=0x%lx\n", indent_level, "", shdr->sh_addr);
+    fprintf(f, "%*ssh_offset=0x%lx\n", indent_level, "", shdr->sh_offset);
+    fprintf(f, "%*ssh_size=0x%lx\n", indent_level, "", shdr->sh_size);
+    fprintf(f, "%*ssh_link=%lu\n", indent_level, "", shdr->sh_link);
+    fprintf(f, "%*ssh_info=%lu\n", indent_level, "", shdr->sh_info);
+    fprintf(f, "%*ssh_addralign=%lu\n", indent_level, "", shdr->sh_addralign);
+    fprintf(f, "%*ssh_entsize=%lu\n", indent_level, "", shdr->sh_entsize);
+}
+
+/* Converts an ELF-32 relocation (non-addend) entry into little endian bytes */
+void
+elf32_write_rel(unsigned char **bufp, Elf32_Rel *rel)
+{
+    YASM_WRITE_32_L(*bufp, rel->r_offset);
+    YASM_WRITE_32_L(*bufp, rel->r_info);
+}
+
+/* Converts an ELF-32 symbol table entry into little endian bytes */
+void
+elf32_write_sym(unsigned char **bufp, Elf32_Sym *sym)
+{
+    YASM_WRITE_32_L(*bufp, sym->st_name);
+    YASM_WRITE_32_L(*bufp, sym->st_value);
+    YASM_WRITE_32_L(*bufp, sym->st_size);
+    YASM_WRITE_8(*bufp, sym->st_info);
+    YASM_WRITE_8(*bufp, sym->st_other);
+    YASM_WRITE_16_L(*bufp, sym->st_shndx);
+}
index 2e80f84dd856448bb710eda3af2d397e155fdbbc..bacb661347a72c9a12e1814e8f0a0e826b338a3e 100644 (file)
@@ -152,4 +152,10 @@ typedef struct {
 /* Macro for constructing st_info from field values. */
 #define ELF32_ST_INFO(bind, type)      (((bind) << 4) + ((type) & 0xf))
 
+void elf32_write_elf_header(unsigned char **bufp, Elf32_Ehdr *ehdr);
+void elf32_write_sect_header(unsigned char **bufp, Elf32_Shdr *shdr);
+void elf32_print_sect_header(FILE *f, int indent_level, Elf32_Shdr *shdr);
+void elf32_write_rel(unsigned char **bufp, Elf32_Rel *rel);
+void elf32_write_sym(unsigned char **bufp, Elf32_Sym *sym);
+
 #endif /* !YASM_ELF32_H */