Commits

insolor committed 977dfad

Some work on ELF supprt

Comments (0)

Files changed (5)

     E_PHNUM     = #2C, -- + 2
     E_SHENTSIZE = #2E, -- + 2 -- Размер структуры, описывающей каждую секцию
     E_SHNUM     = #30, -- + 2 -- Количество секций
-    SIZEOF_EHDR = #32  -- 50
+    E_SHSTRNDX  = #32, -- + 2 -- Индекс таблицы имен в таблице секций
+    SIZEOF_EHDR = #34  -- 52
 
 -- Values of the e_type field
 public enum
     SHT_LOUSER = #80000000,
     SHT_HIUSER = #FFFFFFFF
 
-public constant
-    SHF_WRITE       = #1,
-    SHF_ALLOC       = #2,
-    SHF_EXECINSTR   = #4,
-    SHF_MASKPROC    = #F0000000
+public enum by * 2
+    SHF_WRITE = #1, SHF_ALLOC, SHF_EXECINSTR,
+    SHF_MASKPROC = #F0000000
 
 include patcher.e
 public include std/io.e
         return -1
     end if
 end function
+
+public enum
+    SECTION_NAME,       -- section name
+    SECTION_TYPE,       -- section type
+    SECTION_FLAGS,      -- section flags
+    SECTION_ADDRESS,    -- section virtual address
+    SECTION_OFFSET,     -- section phisical offset
+    SECTION_SIZE,       -- section phisical or virtual size
+    SECTION_LINK,       -- section header table index link
+    SECTION_INFO,       -- section additional info
+    SECTION_ADDRALIGN,  -- section address alignment
+    SECTION_ENTSIZE,    -- section entry size
+    $
+
+public function get_section_table(atom fn)
+    atom section_header_off = fpeek4u(fn, E_SHOFF)
+    integer n = fpeek2u(fn, E_SHNUM)
+    sequence section_table = repeat(0,n)
+    seek(fn, section_header_off)
+    for i = 1 to n do
+        section_table[i] = get_dwords(fn, 10)
+    end for
+    atom strtab = fpeek2u(fn, E_SHSTRNDX)
+    if strtab != SHN_UNDEF then
+        atom strtab_off = section_table[strtab+1][SECTION_OFFSET]
+        for i = 1 to n do
+            sequence name = fpeek_string(fn,strtab_off+section_table[i][SECTION_NAME])
+            section_table[i][SECTION_NAME] = name
+        end for
+    end if
+    return section_table
+end function
 constant byte_order = {"least significant byte first", "most significant byte first"}
 printf(1, " EI_DATA: %s\n",{byte_order[getc(fn)]})
 printf(1, " EI_VERSION: %d\n", getc(fn))
-printf(1, " EI_PAD: %d\n", getc(fn))
+puts(1, "Padding bytes:\n")
+? get_bytes(fn,9)
 
+puts(1, '\n')
+constant e_types = {"ET_NONE","ET_REL","ET_EXEC","ET_DYN","ET_CORE"}
+printf(1, "e_type: %s\n", {e_types[get_integer16(fn)+1]})
+constant e_machines = {
+    "None",
+    "AT&T WE 32100",
+    "SPARC",
+    "Intel 80386",
+    "Motorola 68000",
+    "Motorola 88000",
+    "Intel 80860",
+    "MIPS RS3000"
+}
+printf(1, "e_machine: %s\n", {e_machines[get_integer16(fn)+1]})
+constant e_versions = {"None","Current"}
+printf(1, "e_version: %s\n", {e_versions[get_integer32(fn)+1]})
+printf(1, "e_entry: #%x\n", get_integer32(fn))
+printf(1, "e_phoff: #%x\n", get_integer32(fn))
+printf(1, "e_shoff: #%x\n", get_integer32(fn))
+printf(1, "e_flags: #%x\n", get_integer32(fn))
+printf(1, "e_ehsize: #%x\n", get_integer16(fn))
+printf(1, "e_phentsize: #%x\n", get_integer16(fn))
+printf(1, "e_phnum: #%x\n", get_integer16(fn))
+printf(1, "e_shentsize: #%x\n", get_integer16(fn))
+printf(1, "e_shnum: #%x\n", get_integer16(fn))
+printf(1, "e_shstrndx: #%x\n", get_integer16(fn))
+puts(1,'\n')
+
+sequence sections = get_section_table(fn)
+puts(1,"       Name          Type     Flags   Address  Offset    Size     Link     Info    Align  Ent. size\n")
+for i = 1 to length(sections) do
+    printf(1,"%-18s %08x %08x %08x %08x %08x %08x %08x %08x %08x\n", sections[i])
+end for
 
 
 wait_key()
     return x
 end function
 
+constant MAX_LEN = #10000
+public
+function fpeek_string(atom fn, atom off)
+    sequence s = {}
+    seek(fn,off)
+    for i = 0 to MAX_LEN-1 do
+        integer c = getc(fn)
+        if c = 0 then
+            return s
+        end if
+        s &= c
+    end for
+    return -1 -- too long string
+end function
+
 public
 procedure fpoke(atom fn, atom off, object x)
     seek(fn,off)
         pe = fpeek4u(fn, MZ_LFANEW)
     end if
     n = fpeek2u(fn, pe + PE_NUMBER_OF_SECTIONS)
-    sequence sh = repeat(repeat(0,10),n)
+    sequence section_table = repeat(repeat(0,10),n)
     seek(fn, pe + SIZEOF_PE_HEADER)
     for i = 1 to n do
-        sh[i][1] = get_bytes(fn,8) -- Name
-        if sh[i][$] = 0 then -- Truncate trailing zeros
-            sh[i][1] = sh[i][1][1..find(0,sh[i][1])-1]
+        section_table[i][1] = get_bytes(fn,8) -- Name
+        if section_table[i][$] = 0 then -- Truncate trailing zeros
+            section_table[i][1] = section_table[i][1][1..find(0,section_table[i][1])-1]
         end if
         for j = 2 to 7 do
-            sh[i][j] = get_integer32(fn)
+            section_table[i][j] = get_integer32(fn)
         end for
-        sh[i][8] = get_integer16(fn)
-        sh[i][9] = get_integer16(fn)
-        sh[i][10] = get_integer32(fn)
+        section_table[i][8] = get_integer16(fn)
+        section_table[i][9] = get_integer16(fn)
+        section_table[i][10] = get_integer32(fn)
     end for
-    return sh
+    return section_table
 end function
 
 public
 
 include std/io.e
 
-seek(fn, sections[1][SECTION_POFFSET])
+/*seek(fn, sections[1][SECTION_POFFSET])
 object buf
 for i = 1 to floor((sections[1][SECTION_PSIZE]+blocksize-1)/blocksize) do
     buf = get_bytes(fn,blocksize)
             j+=1
         end while
     end if
-end for
+end for*/
 
 close(fn)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.