[syslinux:slzm] slzm: Syslinux compressed modules

syslinux-bot for H. Peter Anvin hpa at linux.intel.com
Thu Jan 17 21:06:02 PST 2013


Commit-ID:  02b09a09186a36381b3cbceb484d836745cd2b63
Gitweb:     http://www.syslinux.org/commit/02b09a09186a36381b3cbceb484d836745cd2b63
Author:     H. Peter Anvin <hpa at linux.intel.com>
AuthorDate: Thu, 17 Jan 2013 21:02:18 -0800
Committer:  H. Peter Anvin <hpa at linux.intel.com>
CommitDate: Thu, 17 Jan 2013 21:03:53 -0800

slzm: Syslinux compressed modules

Use ELF compressed into a custom lzo container.  The reason for not
using the standard lzop container is that we are doing one-shot
decompression anyway, and by having the appropriate information in the
header we can manage memory better.

Signed-off-by: H. Peter Anvin <hpa at linux.intel.com>

---
 Makefile                                           |   2 +-
 com32/cmenu/Makefile                               |   2 +-
 com32/hdt/Makefile                                 |   2 +-
 com32/include/sys/module.h                         |   5 +-
 .../ldlinux/adv.c => include/syslinux/slzm.h}      |  44 +++----
 com32/lib/Makefile                                 |   4 +-
 com32/lib/sys/module/common.c                      | 123 +++++++++++++------
 com32/lib/sys/module/elf_module.c                  |  31 ++---
 com32/lua/src/Makefile                             |   6 +-
 com32/rosh/Makefile                                |   2 +-
 com32/samples/Makefile                             |   2 +-
 .../elflink/ldlinux/advwrite.c => core/lzo/lzoif.c |  25 ++--
 lzo/.gitignore                                     |   1 +
 lzo/Makefile                                       |   5 +-
 lzo/{prepcore.c => mkmodule.c}                     | 133 +++++++++------------
 mk/devel.mk                                        |   2 +-
 mk/elf.mk                                          |  11 +-
 mk/lib.mk                                          |   4 +-
 mk/syslinux.mk                                     |   2 +
 19 files changed, 223 insertions(+), 183 deletions(-)

diff --git a/Makefile b/Makefile
index 07a1dc3..c9d33c4 100644
--- a/Makefile
+++ b/Makefile
@@ -57,7 +57,7 @@ BOBJECTS = $(BTARGET) \
 # Note: libinstaller is both a BSUBDIR and an ISUBDIR.  It contains
 # files that depend only on the B phase, but may have to be regenerated
 # for "make installer".
-BSUBDIRS = codepage com32 lzo core memdisk mbr memdump gpxe sample \
+BSUBDIRS = lzo codepage com32 core memdisk mbr memdump gpxe sample \
 	   diag libinstaller dos win32 win64 dosutil
 ITARGET  =
 IOBJECTS = $(ITARGET) \
diff --git a/com32/cmenu/Makefile b/com32/cmenu/Makefile
index d51b2e8..7b9e72c 100644
--- a/com32/cmenu/Makefile
+++ b/com32/cmenu/Makefile
@@ -29,7 +29,7 @@ CFLAGS	  += -I./libmenu
 
 LIBMENU = libmenu/syslnx.o libmenu/com32io.o libmenu/tui.o \
 	libmenu/menu.o libmenu/passwords.o libmenu/des.o libmenu/help.o \
-	$(com32)/libutil/libutil.c32 $(com32)/lib/libcom32.c32
+	$(com32)/libutil/libutil.elf $(com32)/lib/libcom32.elf
 
 CMENUS = $(patsubst %.c,%.c32,$(wildcard *.c))
 IMENUS = $(patsubst %.menu,%.c32,$(wildcard *.menu))
diff --git a/com32/hdt/Makefile b/com32/hdt/Makefile
index 42f5c0d..b4df63a 100644
--- a/com32/hdt/Makefile
+++ b/com32/hdt/Makefile
@@ -20,7 +20,7 @@ MAKEDIR = $(topdir)/mk
 include $(MAKEDIR)/elf.mk
 
 LIBS      = ../libupload/libcom32upload.a
-C_LIBS    += $(com32)/cmenu/libmenu/libmenu.c32
+C_LIBS    += $(com32)/cmenu/libmenu/libmenu.elf
 CFLAGS    += -I$(com32)/cmenu/libmenu -I$(com32)
 
 MODULES	  = hdt.c32
diff --git a/com32/include/sys/module.h b/com32/include/sys/module.h
index a18561a..095f394 100644
--- a/com32/include/sys/module.h
+++ b/com32/include/sys/module.h
@@ -109,8 +109,9 @@ struct elf_module {
 	union {
 		// Transient - Data available while the module is loading
 		struct {
-			FILE		*_file;		// The file object of the open file
-			Elf32_Off	_cr_offset;	// The current offset in the open file
+			void		*buf;		// Buffer to data
+			size_t		len;		// Length of data
+			size_t		cr_offset;	// The current offset
 		} l;
 
 		// Process execution data
diff --git a/com32/elflink/ldlinux/adv.c b/com32/include/syslinux/slzm.h
similarity index 67%
copy from com32/elflink/ldlinux/adv.c
copy to com32/include/syslinux/slzm.h
index 4c3ad50..9f06983 100644
--- a/com32/elflink/ldlinux/adv.c
+++ b/com32/include/syslinux/slzm.h
@@ -1,6 +1,6 @@
 /* ----------------------------------------------------------------------- *
  *
- *   Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
+ *   Copyright 2013 Intel Corporation; author: H. Peter Anvin
  *
  *   Permission is hereby granted, free of charge, to any person
  *   obtaining a copy of this software and associated documentation
@@ -26,30 +26,32 @@
  * ----------------------------------------------------------------------- */
 
 /*
- * syslinux/adv.c
+ * slzm.h
  *
- * Access the syslinux auxilliary data vector
+ * Syslinux compressed header format
  */
+#ifndef _SYSLINUX_SLZM_H
+#define _SYSLINUX_SLZM_H
 
-#include <syslinux/adv.h>
-#include <klibc/compiler.h>
 #include <inttypes.h>
-#include <com32.h>
 
-__export void *__syslinux_adv_ptr;
-__export size_t __syslinux_adv_size;
+#define SLZM_MAGIC1 0xcd4cfdb8
+#define SLZM_MAGIC2 0x464c4521
+/* Need to be smarter about these... */
+#define SLZM_PLATFORM 1	/* PC-BIOS */
+#define SLZM_ARCH     3 /* EM_386 */
+#define SLZM_MINVER   0x501
+#define SLZM_MAXVER   0x5ff
 
-extern void adv_init(void);
-void __constructor __syslinux_init(void)
-{
-    static com32sys_t reg;
+struct slzm_header {
+    uint32_t magic[2];
+    uint16_t platform;
+    uint16_t arch;
+    uint16_t minversion;
+    uint16_t maxversion;
+    uint32_t zsize;
+    uint32_t usize;
+    uint32_t resv[6];
+};
 
-    /* Initialize the ADV structure */
-    reg.eax.w[0] = 0x0025;
-    __intcall(0x22, &reg, NULL);
-
-    reg.eax.w[0] = 0x001c;
-    __intcall(0x22, &reg, &reg);
-    __syslinux_adv_ptr = MK_PTR(reg.es, reg.ebx.w[0]);
-    __syslinux_adv_size = reg.ecx.w[0];
-}
+#endif /* _SYSLINUX_SLZM_H */
diff --git a/com32/lib/Makefile b/com32/lib/Makefile
index d979ab4..7bedc22 100644
--- a/com32/lib/Makefile
+++ b/com32/lib/Makefile
@@ -60,7 +60,7 @@ LIBLOAD_OBJS = \
 	syslinux/run_default.o syslinux/run_command.o			\
 	syslinux/cleanup.o syslinux/localboot.o	syslinux/runimage.o	\
 	\
-	syslinux/loadfile.o syslinux/floadfile.o syslinux/zloadfile.o	\
+	syslinux/loadfile.o syslinux/zloadfile.o	\
 	\
 	syslinux/load_linux.o syslinux/initramfs.o			\
 	syslinux/initramfs_file.o syslinux/initramfs_loadfile.o		\
@@ -81,10 +81,10 @@ LIBENTRY_OBJS = \
 	dprintf.o vdprintf.o						\
 	\
 	syslinux/idle.o							\
-	\
 	exit.o
 
 LIBMODULE_OBJS = \
+	syslinux/floadfile.o 						\
 	sys/module/common.o sys/module/elf_module.o		\
 	sys/module/elfutils.o					\
 	sys/module/exec.o
diff --git a/com32/lib/sys/module/common.c b/com32/lib/sys/module/common.c
index deb2bcc..1daa533 100644
--- a/com32/lib/sys/module/common.c
+++ b/com32/lib/sys/module/common.c
@@ -9,6 +9,8 @@
 #include <elf.h>
 #include <string.h>
 #include <fs.h>
+#include <syslinux/loadfile.h>
+#include <syslinux/slzm.h>
 
 #include <linux/list.h>
 #include <sys/module.h>
@@ -16,6 +18,10 @@
 #include "elfutils.h"
 #include "common.h"
 
+__extern int lzo1x_decompress_fast_safe(const void *src, unsigned int src_len,
+					void *dst, unsigned int *dst_len,
+					void *wrkmem);
+
 /**
  * The one and only list of loaded modules
  */
@@ -103,71 +109,120 @@ again:
 
 int image_load(struct elf_module *module)
 {
-	module->u.l._file = findpath(module->name);
+	void *zdata, *mdata;
+	size_t zlen, mlen;
+	const struct slzm_header *hdr;
+	FILE *f;
+	int zr;
+	int rv = -1;
+
+	zdata = mdata = NULL;
+	f = NULL;
+
+	memset(&module->u.l, 0, sizeof module->u.l);
+
+	f = findpath(module->name);
 
-	if (module->u.l._file == NULL) {
-		DBG_PRINT("Could not open object file '%s'\n", module->name);
+	if (!f) {
+		DBG_PRINT("Could not open module file '%s'\n", module->name);
 		goto error;
 	}
 
-	module->u.l._cr_offset = 0;
+	if (floadfile(f, &zdata, &zlen, NULL, 0)) {
+		DBG_PRINT("Could not read module file '%s'\n", module->name);
+		goto error;
+	}
 
-	return 0;
+	fclose(f);
+	f = NULL;
 
-error:
-	if (module->u.l._file != NULL) {
-		fclose(module->u.l._file);
-		module->u.l._file = NULL;
+	hdr = zdata;
+	if (zlen < sizeof *hdr) {
+		dprintf("%s: file too short\n", module->name);
+		goto error;
 	}
 
-	return -1;
-}
+	zlen -= sizeof *hdr;
 
+	if (hdr->magic[0] != SLZM_MAGIC1 || hdr->magic[1] != SLZM_MAGIC2 ||
+	    hdr->platform != SLZM_PLATFORM || hdr->arch != SLZM_ARCH ||
+	    zlen < hdr->zsize) {
+		dprintf("%s: bad header\n", module->name);
+		goto error;
+	}
 
-int image_unload(struct elf_module *module) {
-	if (module->u.l._file != NULL) {
-		fclose(module->u.l._file);
-		module->u.l._file = NULL;
+	mlen = hdr->usize + 15;
+	mdata = malloc(mlen);
+	if (!mdata) {
+		dprintf("%s: failed to allocate mdata buffer\n",
+			module->name);
+		goto error;
+	}
+	
+	zr = lzo1x_decompress_fast_safe((const char *)zdata + sizeof *hdr,
+					hdr->zsize, mdata, &mlen, NULL);
+	if (zr) {
+		dprintf("%s: decompression returned error %d\n",
+			module->name, zr);
+		goto error;
+	}
 
+	if (mlen != hdr->usize) {
+		dprintf("%s: decompression returned %zu bytes expected %u\n",
+			module->name, mlen, hdr->usize);
+		goto error;
 	}
-	module->u.l._cr_offset = 0;
 
+	module->u.l.buf = mdata;
+	module->u.l.len = mlen;
+	mdata = NULL;		/* Don't free */
+	rv = 0;
+
+error:
+	if (mdata)
+		free(mdata);
+	if (zdata)
+		free(zdata);
+	if (f)
+		fclose(f);
+
+	return rv;
+}
+
+
+int image_unload(struct elf_module *module) {
+	if (module->u.l.buf) {
+		free(module->u.l.buf);
+		memset(&module->u.l, 0, sizeof module->u.l);
+	}
 	return 0;
 }
 
 int image_read(void *buff, size_t size, struct elf_module *module) {
-	size_t result = fread(buff, size, 1, module->u.l._file);
-
-	if (result < 1)
+	if (size > module->u.l.len - module->u.l.cr_offset)
 		return -1;
 
-	module->u.l._cr_offset += size;
+	memcpy(buff, (const char *)module->u.l.buf + module->u.l.cr_offset,
+	       size);
+
+	module->u.l.cr_offset += size;
 	return 0;
 }
 
 int image_skip(size_t size, struct elf_module *module) {
-	void *skip_buff = NULL;
-	size_t result;
-
-	if (size == 0)
-		return 0;
-
-	skip_buff = malloc(size);
-	result = fread(skip_buff, size, 1, module->u.l._file);
-	free(skip_buff);
-
-	if (result < 1)
+	if (size > module->u.l.len - module->u.l.cr_offset)
 		return -1;
 
-	module->u.l._cr_offset += size;
+	module->u.l.cr_offset += size;
 	return 0;
 }
 
 int image_seek(Elf32_Off offset, struct elf_module *module) {
-	if (offset < module->u.l._cr_offset) // Cannot seek backwards
+	if (offset >= module->u.l.len) // Cannot seek backwards
 		return -1;
 
-	return image_skip(offset - module->u.l._cr_offset, module);
+	module->u.l.cr_offset = offset;
+	return 0;
 }
 
 
diff --git a/com32/lib/sys/module/elf_module.c b/com32/lib/sys/module/elf_module.c
index 24da220..391be48 100644
--- a/com32/lib/sys/module/elf_module.c
+++ b/com32/lib/sys/module/elf_module.c
@@ -132,28 +132,15 @@ static int load_segments(struct elf_module *module, Elf32_Ehdr *elf_hdr) {
 
 		if (cr_pht->p_type == PT_LOAD) {
 			// Copy the segment at its destination
-			if (cr_pht->p_offset < module->u.l._cr_offset) {
-				// The segment contains data before the current offset
-				// It can be discarded without worry - it would contain only
-				// headers
-				Elf32_Off aux_off = module->u.l._cr_offset - cr_pht->p_offset;
-
-				if (image_read((char *)module_get_absolute(cr_pht->p_vaddr, module) + aux_off,
-					       cr_pht->p_filesz - aux_off, module) < 0) {
-					res = -1;
-					goto out;
-				}
-			} else {
-				if (image_seek(cr_pht->p_offset, module) < 0) {
-					res = -1;
-					goto out;
-				}
-
-				if (image_read(module_get_absolute(cr_pht->p_vaddr, module),
-						cr_pht->p_filesz, module) < 0) {
-					res = -1;
-					goto out;
-				}
+			if (image_seek(cr_pht->p_offset, module) < 0) {
+				res = -1;
+				goto out;
+			}
+			
+			if (image_read(module_get_absolute(cr_pht->p_vaddr, module),
+				       cr_pht->p_filesz, module) < 0) {
+				res = -1;
+				goto out;
 			}
 
 			/*
diff --git a/com32/lua/src/Makefile b/com32/lua/src/Makefile
index d70d23e..b412af1 100644
--- a/com32/lua/src/Makefile
+++ b/com32/lua/src/Makefile
@@ -25,12 +25,12 @@ LNXLIBS	   =
 #GCCWARN	  += -Wno-error
 
 CFLAGS    += -DSYSLINUX
-MODULES	  = lua.c32
+MODULES	  = lua.c32 liblua.c32
 TESTFILES =
 
 OBJS	  = lua.o
 
-LIBLUA    = liblua.c32
+LIBLUA    = liblua.elf
 
 LIBLUA_OBJS := lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o
 LIBLUA_OBJS += lgc.o llex.o lmem.o  lobject.o lopcodes.o lparser.o
@@ -50,7 +50,7 @@ CFLAGS += -DLUA_ANSI
 
 all: $(MODULES) $(TESTFILES)
 
-liblua.elf : $(LIBLUA_OBJS)
+$(LIBLUA) : $(LIBLUA_OBJS)
 	$(LD) $(LDFLAGS) -shared -soname $(patsubst %.elf,%.c32,$(@F)) \
 		-o $@ $^
 
diff --git a/com32/rosh/Makefile b/com32/rosh/Makefile
index 5b54225..f0aa913 100644
--- a/com32/rosh/Makefile
+++ b/com32/rosh/Makefile
@@ -16,7 +16,7 @@
 ## ROSH Read Only Shell
 ##
 
-LIBS = $(com32)/libutil/libutil.c32 $(com32)/lib/libcom32.c32
+LIBS = $(com32)/libutil/libutil.elf $(com32)/lib/libcom32.elf
 
 topdir = ../..
 MAKEDIR = $(topdir)/mk
diff --git a/com32/samples/Makefile b/com32/samples/Makefile
index f6ae00a..781c1f9 100644
--- a/com32/samples/Makefile
+++ b/com32/samples/Makefile
@@ -14,7 +14,7 @@
 ## samples for syslinux users
 ##
 
-LIBS = $(com32)/libutil/libutil.c32
+LIBS = $(com32)/libutil/libutil.elf
 
 topdir = ../..
 MAKEDIR = $(topdir)/mk
diff --git a/com32/elflink/ldlinux/advwrite.c b/core/lzo/lzoif.c
similarity index 70%
copy from com32/elflink/ldlinux/advwrite.c
copy to core/lzo/lzoif.c
index 35829c1..187dd20 100644
--- a/com32/elflink/ldlinux/advwrite.c
+++ b/core/lzo/lzoif.c
@@ -1,6 +1,6 @@
 /* ----------------------------------------------------------------------- *
  *
- *   Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
+ *   Copyright 2013 Intel Corporation; author: H. Peter Anvin
  *
  *   Permission is hereby granted, free of charge, to any person
  *   obtaining a copy of this software and associated documentation
@@ -26,20 +26,21 @@
  * ----------------------------------------------------------------------- */
 
 /*
- * syslinux/advwrite.c
+ * core/lzo/lzoif.c
  *
- * Write back the ADV
+ * This only applies to i386 platforms...
  */
 
-#include <syslinux/adv.h>
-#include <klibc/compiler.h>
-#include <com32.h>
+#include "core.h"
 
-__export int syslinux_adv_write(void)
-{
-    static com32sys_t reg;
+extern int __attribute__((regparm(0)))
+_lzo1x_decompress_asm_fast_safe(const void *src, unsigned int src_len,
+				void *dst, unsigned int *dst_len,
+				void *wrkmem);
 
-    reg.eax.w[0] = 0x001d;
-    __intcall(0x22, &reg, &reg);
-    return (reg.eflags.l & EFLAGS_CF) ? -1 : 0;
+__export int lzo1x_decompress_fast_safe(const void *src, unsigned int src_len,
+			       void *dst, unsigned int *dst_len,
+			       void *wrkmem)
+{
+    return _lzo1x_decompress_asm_fast_safe(src, src_len, dst, dst_len, wrkmem);
 }
diff --git a/lzo/.gitignore b/lzo/.gitignore
index 04202da..910ffde 100644
--- a/lzo/.gitignore
+++ b/lzo/.gitignore
@@ -1 +1,2 @@
 /prepcore
+/mkmodule
diff --git a/lzo/Makefile b/lzo/Makefile
index cf8f985..fe4311d 100644
--- a/lzo/Makefile
+++ b/lzo/Makefile
@@ -18,7 +18,7 @@ INCLUDES += -I./include
 
 LIBOBJS = $(patsubst %.c,%.o,$(wildcard src/*.c))
 LIB     = lzo.a
-BINS    = prepcore
+BINS    = prepcore mkmodule
 
 all : $(BINS)
 
@@ -30,6 +30,9 @@ $(LIB) : $(LIBOBJS)
 prepcore : prepcore.o $(LIB)
 	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
 
+mkmodule : mkmodule.o $(LIB)
+	$(CC) $(LDFLAGS) -o $@ $^ $(LIBS)
+
 tidy dist clean spotless:
 	rm -f $(BINS)
 	rm -f *.o *.a .*.d
diff --git a/lzo/prepcore.c b/lzo/mkmodule.c
similarity index 77%
copy from lzo/prepcore.c
copy to lzo/mkmodule.c
index 9147b2e..9524b32 100644
--- a/lzo/prepcore.c
+++ b/lzo/mkmodule.c
@@ -1,6 +1,6 @@
 /* ----------------------------------------------------------------------- *
- *   
- *   Copyright 2009 Intel Corporation; author: H. Peter Anvin
+ *
+ *   Copyright 2009-2013 Intel Corporation; author: H. Peter Anvin
  *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
@@ -10,7 +10,7 @@
  *
  * ----------------------------------------------------------------------- */
 
-/* 
+/*
    This file is based in part on:
 
    precomp2.c -- example program: how to generate pre-compressed data
@@ -54,6 +54,7 @@
 
 #include "lzo/lzoconf.h"
 #include "lzo/lzo1x.h"
+#include "../com32/include/syslinux/slzm.h"
 
 LZO_EXTERN(int)
 lzo1x_999_compress_internal(const lzo_bytep in, lzo_uint in_len,
@@ -98,13 +99,28 @@ lzo1y_999_compress_internal(const lzo_bytep in, lzo_uint in_len,
 # define noreturn void
 #endif
 
-struct prefix {
-    uint32_t pfx_start;
-    uint32_t pfx_compressed;
-    uint32_t pfx_cdatalen;
-    uint32_t pfx_checksum;
-    uint32_t pfx_maxlma;
-};
+static inline uint16_t get_16(const uint16_t * p)
+{
+#if defined(__i386__) || defined(__x86_64__)
+    /* Littleendian and unaligned-capable */
+    return *p;
+#else
+    const uint8_t *pp = (const uint8_t *)p;
+    return (uint16_t) pp[0] + ((uint16_t) pp[1] << 8);
+#endif
+}
+
+static inline void set_16(uint16_t * p, uint16_t v)
+{
+#if defined(__i386__) || defined(__x86_64__)
+    /* Littleendian and unaligned-capable */
+    *p = v;
+#else
+    uint8_t *pp = (uint8_t *) p;
+    pp[0] = (v & 0xff);
+    pp[1] = ((v >> 8) & 0xff);
+#endif
+}
 
 static inline uint32_t get_32(const uint32_t * p)
 {
@@ -171,20 +187,17 @@ int main(int argc, char *argv[])
     const lzo_uint big = 65536L;	/* can result in very slow compression */
     const lzo_uint32 flags = 0x1;
 
-    lzo_bytep in;
     lzo_bytep infile;
-    lzo_uint in_len, infile_len, start, offset, soff;
+    lzo_uint infile_len;
 
     lzo_bytep out;
     lzo_uint out_bufsize;
-    lzo_uint out_len = 0;
-    lzo_uint outfile_len;
+    lzo_uint best_len, out_len = 0;
 
     lzo_bytep test;
 
     lzo_byte wrkmem[LZO1X_999_MEM_COMPRESS];
 
-    lzo_uint best_len;
     int best_lazy = -1;
 
     lzo_uint orig_len;
@@ -194,7 +207,7 @@ int main(int argc, char *argv[])
     FILE *f;
     long l;
 
-    struct prefix *prefix;
+    struct slzm_header header;
 
     progname = argv[0];
     if (argc != 3) {
@@ -206,6 +219,17 @@ int main(int argc, char *argv[])
 	out_name = argv[2];
 
 /*
+ * Step 0: prepare the fixed part of the header
+ */
+    memset(&header, 0, sizeof header);
+    set_32(&header.magic[0], SLZM_MAGIC1);
+    set_32(&header.magic[1], SLZM_MAGIC2);
+    set_16(&header.platform, SLZM_PLATFORM);
+    set_16(&header.arch, SLZM_ARCH);
+    set_16(&header.minversion, SLZM_MINVER);
+    set_16(&header.maxversion, SLZM_MAXVER);
+
+/*
  * Step 1: initialize the LZO library
  */
     if (lzo_init() != LZO_E_OK)
@@ -237,28 +261,23 @@ int main(int argc, char *argv[])
     infile_len = fread(infile, 1, infile_len, f);
     fclose(f);
 
-/*
- * Select the portion which is for compression...
- */
-    prefix = (struct prefix *)infile;
-    start = get_32(&prefix->pfx_start);
-    offset = get_32(&prefix->pfx_compressed);
-    in = infile + offset;
-    in_len = infile_len - offset;
-    best_len = in_len;
+    set_32(&header.usize, infile_len);
 
 /*
  * Step 5: compute a checksum of the uncompressed data
  */
     uncompressed_checksum = lzo_adler32(0, NULL, 0);
-    uncompressed_checksum = lzo_adler32(uncompressed_checksum, in, in_len);
+    uncompressed_checksum = lzo_adler32(uncompressed_checksum,
+					infile, infile_len);
 
 /*
  * Step 6a: compress from `in' to `out' with LZO1X-999
  */
+    best_len = infile_len;
     for (lazy = 0; lazy <= max_try_lazy; lazy++) {
 	out_len = out_bufsize;
-	r = lzo1x_999_compress_internal(in, in_len, out, &out_len, wrkmem,
+	r = lzo1x_999_compress_internal(infile, infile_len,
+					out, &out_len, wrkmem,
 					NULL, 0, 0,
 					lazy, big, big, big, big, flags);
 	if (r != LZO_E_OK)
@@ -274,7 +293,7 @@ int main(int argc, char *argv[])
 /*
  * Step 7: check if compressible
  */
-    if (best_len >= in_len) {
+    if (best_len >= infile_len) {
 	fprintf(stderr, "%s: %s: this file contains incompressible data.",
 		progname, in_name);
 	/* do it anyway */
@@ -284,25 +303,19 @@ int main(int argc, char *argv[])
  * Step 8: compress data again using the best compressor found
  */
     out_len = out_bufsize;
-    r = lzo1x_999_compress_internal(in, in_len, out, &out_len, wrkmem,
+    r = lzo1x_999_compress_internal(infile, infile_len, out, &out_len, wrkmem,
 				    NULL, 0, 0,
 				    best_lazy, big, big, big, big, flags);
     assert(r == LZO_E_OK);
     assert(out_len == best_len);
+    set_32(&header.zsize, out_len);
 
 /*
  * Step 9: optimize compressed data (compressed data is in `out' buffer)
  */
-#if 1
-    /* Optimization does not require any data in the buffer that will
-     * hold the uncompressed data. To prove this, we clear the buffer.
-     */
-    memset(in, 0, in_len);
-#endif
-
-    orig_len = in_len;
-    r = lzo1x_optimize(out, out_len, in, &orig_len, NULL);
-    if (r != LZO_E_OK || orig_len != in_len) {
+    orig_len = infile_len;
+    r = lzo1x_optimize(out, out_len, infile, &orig_len, NULL);
+    if (r != LZO_E_OK || orig_len != infile_len) {
 	/* this should NEVER happen */
 	error("internal error - optimization failed: %d", r);
     }
@@ -316,59 +329,29 @@ int main(int argc, char *argv[])
 /*
  * Step 11: write compressed data to a file
  */
-    /* Make sure we have up to 2048 bytes of zero after the output */
-    memset(out + out_len, 0, 2048);
-
-    outfile_len = out_len;
-
-    soff = get_32(&prefix->pfx_cdatalen);
-    set_32((uint32_t *) (infile + soff), out_len);
-
-    soff = get_32(&prefix->pfx_checksum);
-    if (soff) {
-	/* ISOLINUX padding and checksumming */
-	uint32_t csum = 0;
-	unsigned int ptr;
-	outfile_len =
-	    ((offset - start + out_len + 2047) & ~2047) - (offset - start);
-	for (ptr = 64; ptr < offset; ptr += 4)
-	    csum += get_32((uint32_t *) (infile + ptr));
-	for (ptr = 0; ptr < outfile_len; ptr += 4)
-	    csum += get_32((uint32_t *) (out + ptr));
-
-	set_32((uint32_t *) (infile + soff), offset - start + outfile_len);
-	set_32((uint32_t *) (infile + soff + 4), csum);
-    }
-
-	/*
-    if (offset+outfile_len > get_32(&prefix->pfx_maxlma))
-	error("output too big (%lu, max %lu)",
-	      (unsigned long)offset+outfile_len,
-	      (unsigned long)get_32(&prefix->pfx_maxlma));
-	      */
-
     f = fopen(out_name, "wb");
     if (!f)
 	error("cannot open output file %s: %s", out_name, strerror(errno));
 
-    if (fwrite(infile + start, 1, offset - start, f) != offset - start ||
-	fwrite(out, 1, outfile_len, f) != outfile_len || fclose(f))
+    if (fwrite(&header, 1, sizeof header, f) != sizeof header ||
+	fwrite(out, 1, out_len, f) != out_len ||
+	fclose(f))
 	error("write error");
 
 /*
  * Step 12: verify decompression
  */
 #ifdef PARANOID
-    orig_len = in_len * 2;
+    orig_len = infile_len * 2;
     test = xzalloc(orig_len);
     r = lzo1x_decompress_safe(out, out_len, test, &orig_len, NULL);
 
-    if (r != LZO_E_OK || orig_len != in_len) {
+    if (r != LZO_E_OK || orig_len != infile_len) {
 	/* this should NEVER happen */
 	error("internal error - decompression failed: %d", r);
     }
 
-    if (memcmp(test, in, in_len)) {
+    if (memcmp(test, infile, infile_len)) {
 	/* this should NEVER happen */
 	error("internal error - decompression data error");
     }
diff --git a/mk/devel.mk b/mk/devel.mk
index 8184c30..15b2ea6 100644
--- a/mk/devel.mk
+++ b/mk/devel.mk
@@ -1,3 +1,3 @@
 # Useful while doing development, but not for production.
 GCCWARN += -Wno-clobbered 
-# GCCWARN += -DDEBUG_PORT=0x3f8 -DDEBUG=1 
+GCCWARN += -DDEBUG_PORT=0x3f8 -DDEBUG=1 
diff --git a/mk/elf.mk b/mk/elf.mk
index 109e0a6..00f6944 100644
--- a/mk/elf.mk
+++ b/mk/elf.mk
@@ -35,7 +35,7 @@ com32 = $(topdir)/com32
 core = $(topdir)/core
 
 ifneq ($(NOGPL),1)
-GPLLIB     = $(com32)/gpllib/libgpl.c32
+GPLLIB     = $(com32)/gpllib/libgpl.elf
 GPLINCLUDE = -I$(com32)/gplinclude
 else
 GPLLIB     =
@@ -54,8 +54,8 @@ LNXCFLAGS  = -I$(com32)/libutil/include -W -Wall -O -g -D_GNU_SOURCE
 LNXSFLAGS  = -g
 LNXLDFLAGS = -g
 
-C_LIBS	   = $(com32)/libutil/libutil.c32 $(GPLLIB) \
-	     $(com32)/lib/libcom32.c32
+C_LIBS	   = $(com32)/libutil/libutil.elf $(GPLLIB) \
+	     $(com32)/lib/libcom32.elf
 C_LNXLIBS  = $(com32)/libutil/libutil_lnx.a \
 	     $(com32)/elflink/ldlinux/ldlinux_lnx.a
 
@@ -86,4 +86,7 @@ C_LNXLIBS  = $(com32)/libutil/libutil_lnx.a \
 	$(LD) $(LDFLAGS) -o $@ $^
 
 %.c32: %.elf
-	$(OBJCOPY) --strip-debug --strip-unneeded $< $@
+	$(OBJCOPY) --strip-debug --strip-unneeded $< $*.tmp
+	$(MKMODULE) $*.tmp $@
+	rm -f $*.tmp
+
diff --git a/mk/lib.mk b/mk/lib.mk
index ca8e41b..5290dc4 100644
--- a/mk/lib.mk
+++ b/mk/lib.mk
@@ -81,4 +81,6 @@ LDFLAGS	= -m elf_i386 --hash-style=gnu -T $(com32)/lib/elf32.ld
 	$(CC) $(MAKEDEPS) $(CFLAGS) $(SOFLAGS) -S -o $@ $<
 
 %.c32: %.elf
-	$(OBJCOPY) --strip-debug --strip-unneeded $< $@
+	$(OBJCOPY) --strip-debug --strip-unneeded $< $*.tmp
+	$(MKMODULE) $*.tmp $@
+	rm -f $*.tmp
diff --git a/mk/syslinux.mk b/mk/syslinux.mk
index 1378b6d..6205ebc 100644
--- a/mk/syslinux.mk
+++ b/mk/syslinux.mk
@@ -65,6 +65,8 @@ MKISOFS  = mkisofs
 SED	 = sed
 WGET	 = wget
 
+MKMODULE = $(topdir)/lzo/mkmodule
+
 com32    = $(topdir)/com32
 
 # Common warnings we want for all gcc-generated code


More information about the Syslinux-commits mailing list