Changeset 11060


Ignore:
Timestamp:
Oct 12, 2008, 8:09:24 AM (11 years ago)
Author:
gb
Message:

Conditionalize for word-size, endianness, architecture. Seems to
produce a valid ELF symbol file on x8632; haven't tried on PPC Linux.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/source/library/elf.lisp

    r7625 r11060  
    7171
    7272(defun new-elf-file-header (object format type machine)
    73   (let* ((ehdr (#_elf64_newehdr (elf-object-libelf-pointer object))))
     73  (let* ((ehdr (#+64-bit-target #_elf64_newehdr #+32-bit-target #_elf32_newehdr (elf-object-libelf-pointer object))))
    7474    (if (%null-ptr-p ehdr)
    7575      (error "Can't create ELF file header for ~s: ~a"
     
    7777             (libelf-error-string))
    7878      (progn
    79         (setf (paref (pref ehdr :<E>lf64_<E>hdr.e_ident) (:* :unsigned-char) #$EI_DATA) format
    80               (pref ehdr :<E>lf64_<E>hdr.e_machine) machine
    81               (pref ehdr :<E>lf64_<E>hdr.e_type) type
    82               (pref ehdr :<E>lf64_<E>hdr.e_version) *checked-libelf-version*)
    83         (assert-pointer-type ehdr :<E>lf64_<E>hdr)))))
     79        (setf (paref (pref ehdr
     80                           #+64-bit-target :<E>lf64_<E>hdr.e_ident
     81                           #+32-bit-target :<E>lf32_<E>hdr.e_ident) (:* :unsigned-char) #$EI_DATA) format
     82              (pref ehdr
     83                    #+64-bit-target :<E>lf64_<E>hdr.e_machine
     84                    #+32-bit-target :<E>lf32_<E>hdr.e_machine) machine
     85              (pref ehdr
     86                    #+64-bit-target :<E>lf64_<E>hdr.e_type
     87                    #+32-bit-target :<E>lf32_<E>hdr.e_type) type
     88              (pref ehdr
     89                    #+64-bit-target :<E>lf64_<E>hdr.e_version
     90                    #+32-bit-target :<E>lf32_<E>hdr.e_version) *checked-libelf-version*)
     91        (assert-pointer-type ehdr
     92                             #+64-bit-target :<E>lf64_<E>hdr
     93                             #+32-bit-target :<E>lf32_<E>hdr)))))
    8494
    8595(defun new-elf-program-header (object &optional (count 1))
    86   (let* ((phdr (#_elf64_newphdr (elf-object-libelf-pointer object) count)))
     96  (let* ((phdr (#+64-bit-target #_elf64_newphdr #+32-bit-target #_elf32_newphdr (elf-object-libelf-pointer object) count)))
    8797    (if (%null-ptr-p phdr)
    8898      (error "Can't create ELF program header for ~s: ~a"
    8999             (elf-object-pathname object)
    90100             (libelf-error-string))
    91       (assert-pointer-type phdr :<E>lf64_<P>hdr))))
     101      (assert-pointer-type phdr
     102                           #+64-bit-target :<E>lf64_<P>hdr
     103                           #+32-bit-target :<E>lf32_<P>hdr))))
    92104
    93105(defun new-elf-section (object)
     
    100112
    101113(defun elf-section-header-for-section (object section)
    102   (let* ((shdr (#_elf64_getshdr section)))
     114  (let* ((shdr (#+64-bit-target #_elf64_getshdr #+32-bit-target #_elf32_getshdr section)))
    103115    (if (%null-ptr-p shdr)
    104116      (error "Can' obtain ELF section header for ~s: ~a"
    105117             (elf-object-pathname object)
    106118             (libelf-error-string))
    107       (assert-pointer-type shdr :<E>lf64_<S>hdr))))
     119      (assert-pointer-type shdr
     120                           #+64-bit-target :<E>lf64_<S>hdr
     121                           #+32-bit-target :<E>lf32_<S>hdr))))
    108122
    109123(defun elf-data-pointer-for-section (object section)
     
    136150    (subseq (nsubstitute #\0 #\# (nsubstitute #\. #\Space name)) 1)))
    137151
     152#+x8664-target
    138153(defx86lapfunction dynamic-dnode ((x arg_z))
    139154  (movq (% x) (% imm0))
     
    141156  (subq (% arg_y) (% imm0))
    142157  (shrq ($ x8664::dnode-shift) (% imm0))
     158  (box-fixnum imm0 arg_z)
     159  (single-value-return))
     160
     161#+x8632-target
     162(defx8632lapfunction dynamic-dnode ((x arg_z))
     163  (movl (% x) (% imm0))
     164  (ref-global x86::heap-start arg_y)
     165  (subl (% arg_y) (% imm0))
     166  (shrl ($ x8632::dnode-shift) (% imm0))
    143167  (box-fixnum imm0 arg_z)
    144168  (single-value-return))
     
    152176                      (when (>= (dynamic-dnode o) frozen-dnodes)
    153177                        (return-from walk nil))
    154                       (when (typep o 'function-vector)
     178                      (when (typep o
     179                                   #+x8664-target 'function-vector
     180                                   #-x8664-target 'function)
    155181                        (functions (function-vector-to-function o))))
    156182                    ccl::area-dynamic
     
    162188  (let* ((functions (collect-elf-static-functions))
    163189         (n (length functions))
    164          (data (#_calloc (1+ n) (record-length :<E>lf64_<S>ym)))
     190         (data (#_calloc (1+ n) (record-length #+64-bit-target :<E>lf64_<S>ym
     191                                               #+32-bit-target :<E>lf32_<S>ym)))
    165192         (string-table (make-elf-string-table)))
    166193    (declare (fixnum n))
    167194    (do* ((i 0 (1+ i))
    168           (p (%inc-ptr data (record-length :<E>lf64_<S>ym)) (progn (%incf-ptr p (record-length :<E>lf64_<S>ym)) p))
     195          (p (%inc-ptr data
     196                       (record-length #+64-bit-target :<E>lf64_<S>ym
     197                                      #+32-bit-target :<E>lf32_<S>ym))
     198             (progn (%incf-ptr p
     199                               (record-length #+64-bit-target :<E>lf64_<S>ym
     200                                              #+32-bit-target :<E>lf32_<S>ym))
     201                    p))
    169202          (f (pop functions) (pop functions)))
    170203         ((= i n)
    171204          (make-elf-symbol-table :strings string-table :data data :nsyms n))
    172205      (declare (fixnum n))
    173       (setf (pref p :<E>lf64_<S>ym.st_name) (elf-register-string (elf-lisp-function-name f) string-table)
    174             (pref p :<E>lf64_<S>ym.st_info) (logior (ash #$STB_GLOBAL 4) #$STT_FUNC)
    175             (pref p :<E>lf64_<S>ym.st_shndx) section-number
    176             (pref p :<E>lf64_<S>ym.st_value) (%address-of f)
    177             (pref p :<E>lf64_<S>ym.st_size) (1+ (ash (1- (%function-code-words f)) target::word-shift))))))
     206      (setf (pref p
     207                  #+64-bit-target :<E>lf64_<S>ym.st_name
     208                  #+32-bit-target :<E>lf32_<S>ym.st_name)
     209            (elf-register-string (elf-lisp-function-name f) string-table)
     210            (pref p
     211                  #+64-bit-target :<E>lf64_<S>ym.st_info
     212                  #+32-bit-target :<E>lf32_<S>ym.st_info)
     213            (logior (ash #$STB_GLOBAL 4) #$STT_FUNC)
     214            (pref p
     215                  #+64-bit-target :<E>lf64_<S>ym.st_shndx
     216                  #+32-bit-target :<E>lf32_<S>ym.st_shndx) section-number
     217            (pref p
     218                  #+64-bit-target :<E>lf64_<S>ym.st_value
     219                  #+32-bit-target :<E>lf32_<S>ym.st_value) (%address-of f)
     220            (pref p
     221                  #+64-bit-target :<E>lf64_<S>ym.st_size
     222                  #+32-bit-target :<E>lf32_<S>ym.st_size) (1+ (ash (1- (%function-code-words f)) target::word-shift))))))
    178223
    179224(defun elf-section-index (section)
     
    206251         (buf (elf-symbol-table-data symbols))
    207252         (nsyms (elf-symbol-table-nsyms symbols) )
    208          (n (* (1+ nsyms) (record-length :<E>lf64_<S>ym))))
     253         (n (* (1+ nsyms) (record-length #+64-bit-target :<E>lf64_<S>ym
     254                                         #+32-bit-target :<E>lf32_<S>ym))))
    209255    (setf (pref symbols-data :<E>lf_<D>ata.d_align) 8
    210256          (pref symbols-data :<E>lf_<D>ata.d_off) 0
     
    240286(defun fixup-lisp-section-offset (fd eof sectnum)
    241287  (fd-lseek fd 0 #$SEEK_SET)
    242   (rlet ((fhdr :<E>lf64_<E>hdr)
    243          (shdr :<E>lf64_<S>hdr))
    244     (fd-read fd fhdr (record-length :<E>lf64_<E>hdr))
    245     (let* ((pos (+ (pref fhdr :<E>lf64_<E>hdr.e_shoff)
    246                    (* sectnum (pref fhdr :<E>lf64_<E>hdr.e_shentsize)))))
     288  (rlet ((fhdr #+64-bit-target :<E>lf64_<E>hdr
     289               #+32-bit-target :<E>lf32_<E>hdr)
     290         (shdr #+64-bit-target :<E>lf64_<S>hdr
     291               #+32-bit-target :<E>lf32_<S>hdr))
     292    (fd-read fd fhdr (record-length #+64-bit-target :<E>lf64_<E>hdr
     293                                    #+32-bit-target :<E>lf32_<E>hdr))
     294    (let* ((pos (+ (pref fhdr #+64-bit-target :<E>lf64_<E>hdr.e_shoff
     295                         #+32-bit-target :<E>lf32_<E>hdr.e_shoff)
     296                   (* sectnum (pref fhdr #+64-bit-target :<E>lf64_<E>hdr.e_shentsize
     297                                    #+32-bit-target :<E>lf32_<E>hdr.e_shentsize)))))
    247298      (fd-lseek fd pos #$SEEK_SET)
    248       (fd-read fd shdr (record-length :<E>lf64_<S>hdr))
    249       (setf (pref shdr :<E>lf64_<S>hdr.sh_offset)
     299      (fd-read fd shdr (record-length #+64-bit-target :<E>lf64_<S>hdr
     300                                      #+32-bit-target :<E>lf32_<S>hdr))
     301      (setf (pref shdr #+64-bit-target :<E>lf64_<S>hdr.sh_offset
     302                  #+32-bit-target :<E>lf32_<S>hdr.sh_offset)
    250303            (+ #x2000 (logandc2 (+ eof 4095) 4095))) ; #x2000 for nilreg-area
    251304      (fd-lseek fd pos #$SEEK_SET)
    252       (fd-write fd shdr (record-length :<E>lf64_<S>hdr))
     305      (fd-write fd shdr (record-length #+64-bit-target :<E>lf64_<S>hdr
     306                                       #+32-bit-target :<E>lf32_<S>hdr))
    253307      t)))
    254308 
    255309(defun write-elf-symbols-to-file (pathname)
    256310  (let* ((object (create-elf-object pathname))
    257          (file-header (new-elf-file-header object #$ELFDATA2LSB #$ET_DYN #$EM_X86_64))
     311         (file-header (new-elf-file-header object
     312                                           #+little-endian-target #$ELFDATA2LSB
     313                                           #+big-endian-target #$ELFDATA2MSB
     314                                           #$ET_DYN
     315                                           #+x8664-target #$EM_X86_64
     316                                           #+x8632-target #$EM_386
     317                                           #+ppc32-target #$EM_PPC
     318                                           #+ppc64-target #$EM_PPC64
     319                                           ))
    258320         (program-header (new-elf-program-header object))
    259321         (lisp-section (new-elf-section object))
     
    269331         (shstrtab-section-header (elf-section-header-for-section object shstrtab-section)))
    270332   
    271     (setf (pref file-header :<E>lf64_<E>hdr.e_shstrndx) (elf-section-index shstrtab-section))
    272     (setf (pref lisp-section-header :<E>lf64_<S>hdr.sh_name) (elf-register-string ".lisp" section-names)
    273           (pref lisp-section-header :<E>lf64_<S>hdr.sh_type) #$SHT_NOBITS
    274           (pref lisp-section-header :<E>lf64_<S>hdr.sh_flags) (logior #$SHF_WRITE #$SHF_ALLOC #$SHF_EXECINSTR)
    275           (pref lisp-section-header :<E>lf64_<S>hdr.sh_addr) (ash (%get-kernel-global heap-start) target::fixnumshift)
    276           (pref lisp-section-header :<E>lf64_<S>hdr.sh_size) (ash (frozen-space-dnodes) target::dnode-shift)
    277           (pref lisp-section-header :<E>lf64_<S>hdr.sh_offset) 0
    278           (pref lisp-section-header :<E>lf64_<S>hdr.sh_addralign) 1)
    279     (setf (pref symbols-section-header :<E>lf64_<S>hdr.sh_name) (elf-register-string ".symtab" section-names)
    280           (pref symbols-section-header :<E>lf64_<S>hdr.sh_type) #$SHT_SYMTAB
    281           (pref symbols-section-header :<E>lf64_<S>hdr.sh_entsize) (record-length :<E>lf64_<S>ym)
    282           (pref symbols-section-header :<E>lf64_<S>hdr.sh_link) (elf-section-index strings-section))
    283     (setf (pref strings-section-header :<E>lf64_<S>hdr.sh_name) (elf-register-string ".strtab" section-names)
    284           (pref strings-section-header :<E>lf64_<S>hdr.sh_type) #$SHT_STRTAB
    285           (pref strings-section-header :<E>lf64_<S>hdr.sh_flags) (logior #$SHF_STRINGS #$SHF_ALLOC))
    286     (setf (pref shstrtab-section-header :<E>lf64_<S>hdr.sh_name) (elf-register-string ".shstrtab" section-names)
    287           (pref shstrtab-section-header :<E>lf64_<S>hdr.sh_type) #$SHT_STRTAB
    288           (pref shstrtab-section-header :<E>lf64_<S>hdr.sh_flags) (logior #$SHF_STRINGS #$SHF_ALLOC))
     333    (setf (pref file-header #+64-bit-target :<E>lf64_<E>hdr.e_shstrndx
     334                #+32-bit-target :<E>lf32_<E>hdr.e_shstrndx) (elf-section-index shstrtab-section))
     335    (setf (pref lisp-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_name
     336                #+32-bit-target :<E>lf32_<S>hdr.sh_name) (elf-register-string ".lisp" section-names)
     337          (pref lisp-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_type
     338                #+32-bit-target :<E>lf32_<S>hdr.sh_type) #$SHT_NOBITS
     339          (pref lisp-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_flags
     340                #+32-bit-target :<E>lf32_<S>hdr.sh_flags) (logior #$SHF_WRITE #$SHF_ALLOC #$SHF_EXECINSTR)
     341          (pref lisp-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_addr
     342                #+32-bit-target :<E>lf32_<S>hdr.sh_addr) (ash (%get-kernel-global heap-start) target::fixnumshift)
     343          (pref lisp-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_size
     344                #+32-bit-target :<E>lf32_<S>hdr.sh_size) (ash (frozen-space-dnodes) target::dnode-shift)
     345          (pref lisp-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_offset
     346                #+32-bit-target :<E>lf32_<S>hdr.sh_offset) 0
     347          (pref lisp-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_addralign
     348                #+32-bit-target :<E>lf32_<S>hdr.sh_addralign) 1)
     349    (setf (pref symbols-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_name
     350                #+32-bit-target :<E>lf32_<S>hdr.sh_name) (elf-register-string ".symtab" section-names)
     351          (pref symbols-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_type
     352                #+32-bit-target :<E>lf32_<S>hdr.sh_type) #$SHT_SYMTAB
     353          (pref symbols-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_entsize
     354                #+32-bit-target :<E>lf32_<S>hdr.sh_entsize) (record-length #+64-bit-target :<E>lf64_<S>ym
     355                                                                           #+32-bit-target :<E>lf32_<S>ym)
     356          (pref symbols-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_link
     357                #+32-bit-target :<E>lf32_<S>hdr.sh_link) (elf-section-index strings-section))
     358    (setf (pref strings-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_name
     359                #+32-bit-target :<E>lf32_<S>hdr.sh_name) (elf-register-string ".strtab" section-names)
     360          (pref strings-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_type
     361                #+32-bit-target :<E>lf32_<S>hdr.sh_type) #$SHT_STRTAB
     362          (pref strings-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_flags
     363                #+32-bit-target :<E>lf32_<S>hdr.sh_flags) (logior #$SHF_STRINGS #$SHF_ALLOC))
     364    (setf (pref shstrtab-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_name
     365                #+32-bit-target :<E>lf32_<S>hdr.sh_name) (elf-register-string ".shstrtab" section-names)
     366          (pref shstrtab-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_type
     367                #+32-bit-target :<E>lf32_<S>hdr.sh_type) #$SHT_STRTAB
     368          (pref shstrtab-section-header #+64-bit-target :<E>lf64_<S>hdr.sh_flags
     369                #+32-bit-target :<E>lf32_<S>hdr.sh_flags) (logior #$SHF_STRINGS #$SHF_ALLOC))
    289370    (elf-make-empty-data-for-section object lisp-section (ash (frozen-space-dnodes) target::dnode-shift))
    290371    (elf-init-section-data-from-string-table object strings-section (elf-symbol-table-strings symbols))
     
    294375    (elf-update object #$ELF_C_NULL)
    295376    ;; Fix up the program header.
    296     (setf (pref program-header :<E>lf64_<P>hdr.p_type) #$PT_PHDR
    297           (pref program-header :<E>lf64_<P>hdr.p_offset) (pref file-header :<E>lf64_<E>hdr.e_phoff)
    298           (pref program-header :<E>lf64_<P>hdr.p_filesz) (#_elf64_fsize #$ELF_T_PHDR 1 #$EV_CURRENT))
     377    (setf (pref program-header
     378                #+64-bit-target :<E>lf64_<P>hdr.p_type
     379                #+32-bit-target :<E>lf32_<P>hdr.p_type) #$PT_PHDR
     380          (pref program-header #+64-bit-target :<E>lf64_<P>hdr.p_offset
     381                #+32-bit-target :<E>lf32_<P>hdr.p_offset)
     382          (pref file-header
     383                #+64-bit-target :<E>lf64_<E>hdr.e_phoff
     384                #+32-bit-target :<E>lf32_<E>hdr.e_phoff)
     385          (pref program-header
     386                #+64-bit-target :<E>lf64_<P>hdr.p_filesz
     387                #+32-bit-target :<E>lf32_<P>hdr.p_filesz)
     388          (#+64-bit-target #_elf64_fsize #+32-bit-target #_elf32_fsize #$ELF_T_PHDR 1 #$EV_CURRENT))
    299389    ;; Mark the program header as being dirty.
    300390    (elf-flag-phdr object #$ELF_C_SET #$ELF_F_DIRTY)
Note: See TracChangeset for help on using the changeset viewer.