Re: [PATCH 2/6] ELF refactor and consequent linker simplifications

2013-05-23 Thread Ludovic Courtès
Andy Wingo  skribis:

> On Wed 22 May 2013 22:44, l...@gnu.org (Ludovic Courtès) writes:
>
>>> -(define (fold2 proc ls s0 s1)
>>> -  (let lp ((ls ls) (s0 s0) (s1 s1))
>>> -(if (null? ls)
>>> -(values s0 s1)
>>> -(receive (s0 s1) (proc (car ls) s0 s1)
>>> -  (lp (cdr ls) s0 s1)
>>> -
>>>  (define (fold4 proc ls s0 s1 s2 s3)
>>>(let lp ((ls ls) (s0 s0) (s1 s1) (s2 s2) (s3 s3))
>>>  (if (null? ls)
>>> @@ -236,15 +231,9 @@
>>>  (receive (s0 s1 s2 s3) (proc (car ls) s0 s1 s2 s3)
>>>(lp (cdr ls) s0 s1 s2 s3)
>>>  
>>> -(define (fold5 proc ls s0 s1 s2 s3 s4)
>>> -  (let lp ((ls ls) (s0 s0) (s1 s1) (s2 s2) (s3 s3) (s4 s4))
>>> -(if (null? ls)
>>> -(values s0 s1 s2 s3 s4)
>>> -(receive (s0 s1 s2 s3 s4) (proc (car ls) s0 s1 s2 s3 s4)
>>> -  (lp (cdr ls) s0 s1 s2 s3 s4)
>>
>> What about moving these to a helper module eventually?
>
> Sure.  Or maybe a fold-values that takes a variable number of arguments and
> inlines itself...
>
>   (define-syntax fold-values
> (lambda (x)
>   (syntax-case x ()
> ((_ proc list seed ...)
>  (with-syntax (((s ...) (generate-temporaries #'(seed ...
>#'(let ((p proc))
>(let lp ((l list) (s seed) ...)
>  (match l
>(() (values s ...))
>((elt . l)
> (call-with-values (lambda () (p elt s ...))
>   (lambda (s ...)
> (lp l s ...

Even better.  :-)

Ludo’.



Re: [PATCH 2/6] ELF refactor and consequent linker simplifications

2013-05-23 Thread Andy Wingo
On Wed 22 May 2013 22:44, l...@gnu.org (Ludovic Courtès) writes:

>> -(define (fold2 proc ls s0 s1)
>> -  (let lp ((ls ls) (s0 s0) (s1 s1))
>> -(if (null? ls)
>> -(values s0 s1)
>> -(receive (s0 s1) (proc (car ls) s0 s1)
>> -  (lp (cdr ls) s0 s1)
>> -
>>  (define (fold4 proc ls s0 s1 s2 s3)
>>(let lp ((ls ls) (s0 s0) (s1 s1) (s2 s2) (s3 s3))
>>  (if (null? ls)
>> @@ -236,15 +231,9 @@
>>  (receive (s0 s1 s2 s3) (proc (car ls) s0 s1 s2 s3)
>>(lp (cdr ls) s0 s1 s2 s3)
>>  
>> -(define (fold5 proc ls s0 s1 s2 s3 s4)
>> -  (let lp ((ls ls) (s0 s0) (s1 s1) (s2 s2) (s3 s3) (s4 s4))
>> -(if (null? ls)
>> -(values s0 s1 s2 s3 s4)
>> -(receive (s0 s1 s2 s3 s4) (proc (car ls) s0 s1 s2 s3 s4)
>> -  (lp (cdr ls) s0 s1 s2 s3 s4)
>
> What about moving these to a helper module eventually?

Sure.  Or maybe a fold-values that takes a variable number of arguments and
inlines itself...

  (define-syntax fold-values
(lambda (x)
  (syntax-case x ()
((_ proc list seed ...)
 (with-syntax (((s ...) (generate-temporaries #'(seed ...
   #'(let ((p proc))
   (let lp ((l list) (s seed) ...)
 (match l
   (() (values s ...))
   ((elt . l)
(call-with-values (lambda () (p elt s ...))
  (lambda (s ...)
(lp l s ...

I'll do that :)

A
-- 
http://wingolog.org/



Re: [PATCH 2/6] ELF refactor and consequent linker simplifications

2013-05-22 Thread Ludovic Courtès
Andy Wingo  skribis:

> * module/system/vm/elf.scm: Add commentary.
>   (make-elf): Add a constructor similar to make-elf-segment and
>   make-elf-section.
>   (write-elf32-header, write-elf64-header, write-elf-header): Take an
>instead of all the fields separately.
>   (, ): Add "index" property.  Adapt
>   constructors accordingly.
>
> * module/language/objcode/elf.scm (bytecode->elf): Arrange to set the
>   section indexes when creating ELF sections.
>
> * module/system/vm/linker.scm (alloc-segment, relocate-section-header):
>   Arrange to set segment and section indexes.
>   (find-shstrndx): New helper, replaces compute-sections-by-name.  Now
>   that sections know their indexes, this is easier.
>   (allocate-elf, write-elf): New helpers, factored out of link-elf.
>   Easier now that sections have indexes.
>   (link-elf): Simplify.  Check that the incoming objects have sensible
>   numbers.
>
> * test-suite/tests/linker.test: Update to set #:index on the linker
>   objects.

Looks good.

> -(define (fold2 proc ls s0 s1)
> -  (let lp ((ls ls) (s0 s0) (s1 s1))
> -(if (null? ls)
> -(values s0 s1)
> -(receive (s0 s1) (proc (car ls) s0 s1)
> -  (lp (cdr ls) s0 s1)
> -
>  (define (fold4 proc ls s0 s1 s2 s3)
>(let lp ((ls ls) (s0 s0) (s1 s1) (s2 s2) (s3 s3))
>  (if (null? ls)
> @@ -236,15 +231,9 @@
>  (receive (s0 s1 s2 s3) (proc (car ls) s0 s1 s2 s3)
>(lp (cdr ls) s0 s1 s2 s3)
>  
> -(define (fold5 proc ls s0 s1 s2 s3 s4)
> -  (let lp ((ls ls) (s0 s0) (s1 s1) (s2 s2) (s3 s3) (s4 s4))
> -(if (null? ls)
> -(values s0 s1 s2 s3 s4)
> -(receive (s0 s1 s2 s3 s4) (proc (car ls) s0 s1 s2 s3 s4)
> -  (lp (cdr ls) s0 s1 s2 s3 s4)

What about moving these to a helper module eventually?

Ludo’.




[PATCH 2/6] ELF refactor and consequent linker simplifications

2013-05-18 Thread Andy Wingo
* module/system/vm/elf.scm: Add commentary.
  (make-elf): Add a constructor similar to make-elf-segment and
  make-elf-section.
  (write-elf32-header, write-elf64-header, write-elf-header): Take an
   instead of all the fields separately.
  (, ): Add "index" property.  Adapt
  constructors accordingly.

* module/language/objcode/elf.scm (bytecode->elf): Arrange to set the
  section indexes when creating ELF sections.

* module/system/vm/linker.scm (alloc-segment, relocate-section-header):
  Arrange to set segment and section indexes.
  (find-shstrndx): New helper, replaces compute-sections-by-name.  Now
  that sections know their indexes, this is easier.
  (allocate-elf, write-elf): New helpers, factored out of link-elf.
  Easier now that sections have indexes.
  (link-elf): Simplify.  Check that the incoming objects have sensible
  numbers.

* test-suite/tests/linker.test: Update to set #:index on the linker
  objects.
---
 module/language/objcode/elf.scm |   17 +--
 module/system/vm/elf.scm|  188 +++--
 module/system/vm/linker.scm |  223 +--
 test-suite/tests/linker.test|7 +-
 4 files changed, 238 insertions(+), 197 deletions(-)

diff --git a/module/language/objcode/elf.scm b/module/language/objcode/elf.scm
index 1edfdcf..981c398 100644
--- a/module/language/objcode/elf.scm
+++ b/module/language/objcode/elf.scm
@@ -41,15 +41,16 @@
 (lambda (table idx)
   (set! string-table table)
   idx)))
-(define (make-object name bv relocs . kwargs)
+(define (make-object index name bv relocs . kwargs)
   (let ((name-idx (intern-string! (symbol->string name
 (make-linker-object (apply make-elf-section
+   #:index index
#:name name-idx
#:size (bytevector-length bv)
kwargs)
 bv relocs
 (list (make-linker-symbol name 0)
-(define (make-dynamic-section word-size endianness)
+(define (make-dynamic-section index word-size endianness)
   (define (make-dynamic-section/32)
 (let ((bv (make-bytevector 24 0)))
   (bytevector-u32-set! bv 0 DT_GUILE_RTL_VERSION endianness)
@@ -74,19 +75,19 @@
 ((8) (make-dynamic-section/64))
 (else (error "unexpected word size" word-size
 (lambda (bv reloc)
-  (make-object '.dynamic bv (list reloc)
+  (make-object index '.dynamic bv (list reloc)
#:type SHT_DYNAMIC #:flags SHF_ALLOC
-(define (make-string-table)
+(define (make-string-table index)
   (intern-string! ".shstrtab")
-  (make-object '.shstrtab (link-string-table string-table) '()
+  (make-object index '.shstrtab (link-string-table string-table) '()
#:type SHT_STRTAB #:flags 0))
 (let* ((word-size (target-word-size))
(endianness (target-endianness))
-   (text (make-object '.rtl-text bv '()))
-   (dt (make-dynamic-section word-size endianness))
+   (text (make-object 1 '.rtl-text bv '()))
+   (dt (make-dynamic-section 2 word-size endianness))
;; This needs to be linked last, because linking other
;; sections adds entries to the string table.
-   (shstrtab (make-string-table)))
+   (shstrtab (make-string-table 3)))
   (link-elf (list text dt shstrtab)
 #:endianness endianness #:word-size word-size
 
diff --git a/module/system/vm/elf.scm b/module/system/vm/elf.scm
index e2b2454..efa9782 100644
--- a/module/system/vm/elf.scm
+++ b/module/system/vm/elf.scm
@@ -16,6 +16,19 @@
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 
USA
 
+;;; Commentary:
+;;;
+;;; A module to read and write Executable and Linking Format (ELF)
+;;; files.
+;;;
+;;; This module exports a number of record types that represent the
+;;; various parts that make up ELF files.  Fundamentally this is the
+;;; main header, the segment headers (program headers), and the section
+;;; headers.  It also exports bindings for symbolic constants and
+;;; utilities to parse and write special kinds of ELF sections.
+;;;
+;;; See elf(5) for more information on ELF.
+;;;
 ;;; Code:
 
 (define-module (system vm elf)
@@ -27,7 +40,8 @@
   #:use-module (ice-9 vlist)
   #:export (has-elf-header?
 
-make-elf elf?
+(make-elf* . make-elf)
+elf?
 elf-bytes elf-word-size elf-byte-order
 elf-abi elf-type elf-machine-type
 elf-entry elf-phoff elf-shoff elf-flags elf-ehsize
@@ -37,6 +51,7 @@
 
 (make-elf-segment* . make-elf-segment)
 elf-segment?
+elf-segment-index
 elf-se