mdebug

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 1ce128d22448271b6d991006b58e7348beb21bb2
parent 5f3d92572d243a0df47816971d44f64e6ccad5e7
Author: Brian Swetland <swetland@frotz.net>
Date:   Sun, 14 Jun 2015 21:26:35 -0700

mkbuiltins: tool to bundle binaries into executable

Diffstat:
Atools/mkbuiltins.c | 133+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtools/module.mk | 10++++++++++
2 files changed, 143 insertions(+), 0 deletions(-)

diff --git a/tools/mkbuiltins.c b/tools/mkbuiltins.c @@ -0,0 +1,133 @@ +/* mkbuiltin.c + * + * Copyright 2011 Brian Swetland <swetland@frotz.net> + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <fcntl.h> + +void *load_file(const char *fn, size_t *_sz) { + int fd; + off_t sz; + void *data = NULL; + fd = open(fn, O_RDONLY); + if (fd < 0) goto fail; + sz = lseek(fd, 0, SEEK_END); + if (sz < 0) goto fail; + if (lseek(fd, 0, SEEK_SET)) goto fail; + if ((data = malloc(sz + 4)) == NULL) goto fail; + if (read(fd, data, sz) != sz) goto fail; + *_sz = sz; + return data; +fail: + if (data) free(data); + if (fd >= 0) close(fd); + return NULL; +} + +typedef struct entry { + struct entry *next; + const char *fn; + void *data; + size_t sz; +} entry; + +entry *first = NULL, *last = NULL; + +int import(const char *fn) { + char *x; + entry *e = malloc(sizeof(entry)); + if (e == NULL) return -1; + e->data = load_file(fn, &e->sz); + if (e->data == NULL) return -1; + if ((x = strrchr(fn, '/'))) { + e->fn = x + 1; + } else { + e->fn = fn; + } + e->next = NULL; + if (last) { + last->next = e; + } else { + first = e; + } + last = e; + return 0; +} + +void exportline(unsigned char *x, int len) { + printf("\t\""); + while (len > 0) { + printf("\\x%02X", *x++); + len--; + } + printf("\"\n"); +} + +void export(entry *e) { + unsigned char *x = e->data; + size_t len = e->sz; + printf("\t{ \"%s\", %zu,\n", e->fn, e->sz); + while (len > 0) { + int xfer = (len > 16) ? 16 : len; + exportline(x, xfer); + x += xfer; + len -= xfer; + } + printf("\t},\n"); +} + +int main(int argc, char **argv) { + entry *e; + + while (argc > 1) { + if (import(argv[1])) return -1; + argc--; + argv++; + } + + printf( +"/* this file is machine-generated by mkbuiltins -- do not modify */\n\n" +"#include <string.h>\n" +"#include <stdint.h>\n\n" +"static struct {\n" +" const char *name;\n" +" size_t size;\n" +" void *data;\n" +"} files[] = {\n" + ); + for (e = first; e != NULL; e = e->next) { + export(e); + } + printf( +"};\n\n" +"void *get_builtin_file(const char *name, size_t *sz) {\n" +" int n;\n" +" for (n = 0; n < (sizeof(files)/sizeof(files[0])); n++) {\n" +" if (!strcmp(name, files[n].name)) {\n" +" *sz = files[n].size;\n" +" return files[n].data;\n" +" }\n" +" }\n" +" return NULL;\n" +"}\n" + ); + + return 0; +} + diff --git a/tools/module.mk b/tools/module.mk @@ -7,8 +7,14 @@ M_OBJS += tools/debugger-commands.o M_OBJS += tools/rswdp.o M_OBJS += tools/linenoise.o M_OBJS += tools/usb.o +M_OBJS += out/debugger-builtins.o $(call build-host-executable) +AGENTS := out/agent-lpc13xx.bin out/agent-lpc15xx.bin +out/debugger-builtins.c: $(AGENTS) bin/mkbuiltins + @mkdir -p out + ./bin/mkbuiltins $(AGENTS) > $@ + M_NAME := gdb-bridge M_OBJS := tools/gdb-bridge.o M_OBJS += tools/debugger-core.o @@ -16,6 +22,7 @@ M_OBJS += tools/debugger-commands.o M_OBJS += tools/rswdp.o M_OBJS += tools/linenoise.o M_OBJS += tools/usb.o +M_OBJS += out/debugger-builtins.o $(call build-host-executable) M_NAME := stm32boot @@ -44,3 +51,6 @@ M_NAME := uconsole M_OBJS := tools/uconsole.o tools/usb.o $(call build-host-executable) +M_NAME := mkbuiltins +M_OBJS := tools/mkbuiltins.o +$(call build-host-executable)