Working version
This commit is contained in:
		| @@ -26,6 +26,7 @@ LIBWRAP_SRCS := \ | ||||
| 	sys/times.c \ | ||||
| 	sys/sbrk.c \ | ||||
| 	sys/_exit.c \ | ||||
| 	sys/puts.c \ | ||||
| 	misc/write_hex.c | ||||
|  | ||||
| LIBWRAP_SRCS := $(foreach f,$(LIBWRAP_SRCS),$(LIBWRAP_DIR)/$(f)) | ||||
| @@ -34,13 +35,14 @@ LIBWRAP_OBJS := $(LIBWRAP_SRCS:.c=.o) | ||||
| LIBWRAP_SYMS := malloc free \ | ||||
| 	open lseek read write fstat stat close link unlink \ | ||||
| 	execve fork getpid kill wait \ | ||||
| 	isatty times sbrk _exit | ||||
| 	isatty times sbrk _exit puts | ||||
|  | ||||
| LIBWRAP := libwrap.a | ||||
|  | ||||
| LINK_DEPS += $(LIBWRAP) | ||||
|  | ||||
| LDFLAGS += $(foreach s,$(LIBWRAP_SYMS),-Wl,--wrap=$(s)) | ||||
| LDFLAGS += $(foreach s,$(LIBWRAP_SYMS),-Wl,--wrap=_$(s)) | ||||
| LDFLAGS += -L. -Wl,--start-group -lwrap -lc -Wl,--end-group | ||||
|  | ||||
| CLEAN_OBJS += $(LIBWRAP_OBJS) | ||||
|   | ||||
| @@ -4,13 +4,13 @@ | ||||
| #include <unistd.h> | ||||
| #include "platform.h" | ||||
|  | ||||
| void write_hex(int fd, uint32_t hex) | ||||
| void write_hex(int fd, unsigned long int hex) | ||||
| { | ||||
|   uint8_t ii; | ||||
|   uint8_t jj; | ||||
|   char towrite; | ||||
|   write(fd , "0x", 2); | ||||
|   for (ii = 8 ; ii > 0; ii--) { | ||||
|   for (ii = sizeof(unsigned long int) * 2 ; ii > 0; ii--) { | ||||
|     jj = ii - 1; | ||||
|     uint8_t digit = ((hex & (0xF << (jj*4))) >> (jj*4)); | ||||
|     towrite = digit < 0xA ? ('0' + digit) : ('A' +  (digit - 0xA)); | ||||
|   | ||||
| @@ -2,12 +2,11 @@ | ||||
|  | ||||
| #include <unistd.h> | ||||
| #include "platform.h" | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| void __wrap__exit(int code) | ||||
| void __wrap_exit(int code) | ||||
| { | ||||
| //volatile uint32_t* leds = (uint32_t*) (GPIO_BASE_ADDR + GPIO_OUT_OFFSET); | ||||
|   const char message[] = "\nProgam has exited with code:"; | ||||
| //*leds = (~(code)); | ||||
|  | ||||
|   write(STDERR_FILENO, message, sizeof(message) - 1); | ||||
|   write_hex(STDERR_FILENO, code); | ||||
| @@ -15,3 +14,4 @@ void __wrap__exit(int code) | ||||
|  | ||||
|   for (;;); | ||||
| } | ||||
| weak_under_alias(exit); | ||||
|   | ||||
| @@ -2,8 +2,10 @@ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include "stub.h" | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| int __wrap_close(int fd) | ||||
| { | ||||
|   return _stub(EBADF); | ||||
| } | ||||
| weak_under_alias(close); | ||||
|   | ||||
| @@ -2,8 +2,10 @@ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include "stub.h" | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| int __wrap_execve(const char* name, char* const argv[], char* const env[]) | ||||
| { | ||||
|   return _stub(ENOMEM); | ||||
| } | ||||
| weak_under_alias(execve); | ||||
|   | ||||
| @@ -4,6 +4,7 @@ | ||||
| #include <unistd.h> | ||||
| #include <sys/stat.h> | ||||
| #include "stub.h" | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| int __wrap_fstat(int fd, struct stat* st) | ||||
| { | ||||
| @@ -14,3 +15,4 @@ int __wrap_fstat(int fd, struct stat* st) | ||||
|  | ||||
|   return _stub(EBADF); | ||||
| } | ||||
| weak_under_alias(fstat); | ||||
|   | ||||
| @@ -1,6 +1,8 @@ | ||||
| /* See LICENSE of license details. */ | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| int __wrap_getpid(void) | ||||
| { | ||||
|   return 1; | ||||
| } | ||||
| weak_under_alias(getpid); | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <unistd.h> | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| int __wrap_isatty(int fd) | ||||
| { | ||||
| @@ -9,3 +10,4 @@ int __wrap_isatty(int fd) | ||||
|  | ||||
|   return 0; | ||||
| } | ||||
| weak_under_alias(isatty); | ||||
|   | ||||
| @@ -2,8 +2,10 @@ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include "stub.h" | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| int __wrap_kill(int pid, int sig) | ||||
| { | ||||
|   return _stub(EINVAL); | ||||
| } | ||||
| weak_under_alias(kill); | ||||
|   | ||||
| @@ -2,8 +2,10 @@ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include "stub.h" | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| int __wrap_link(const char *old_name, const char *new_name) | ||||
| { | ||||
|   return _stub(EMLINK); | ||||
| } | ||||
| weak_under_alias(link); | ||||
|   | ||||
| @@ -4,6 +4,7 @@ | ||||
| #include <unistd.h> | ||||
| #include <sys/types.h> | ||||
| #include "stub.h" | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| off_t __wrap_lseek(int fd, off_t ptr, int dir) | ||||
| { | ||||
| @@ -12,3 +13,4 @@ off_t __wrap_lseek(int fd, off_t ptr, int dir) | ||||
|  | ||||
|   return _stub(EBADF); | ||||
| } | ||||
| weak_under_alias(lseek); | ||||
|   | ||||
| @@ -2,8 +2,10 @@ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include "stub.h" | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| int __wrap_open(const char* name, int flags, int mode) | ||||
| { | ||||
|   return _stub(ENOENT); | ||||
| } | ||||
| weak_under_alias(open); | ||||
|   | ||||
| @@ -2,8 +2,10 @@ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include "stub.h" | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| int __wrap_openat(int dirfd, const char* name, int flags, int mode) | ||||
| { | ||||
|   return _stub(ENOENT); | ||||
| } | ||||
| weak_under_alias(openat); | ||||
|   | ||||
							
								
								
									
										28
									
								
								hello-world/bsp/libwrap/sys/puts.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								hello-world/bsp/libwrap/sys/puts.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <stdint.h> | ||||
| #include <errno.h> | ||||
| #include <unistd.h> | ||||
| #include <sys/types.h> | ||||
|  | ||||
| #include "platform.h" | ||||
| #include "stub.h" | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| int __wrap_puts(const char *s) | ||||
| { | ||||
|   while (*s != '\0') { | ||||
|     while (UART0_REG(UART_REG_TXFIFO) & 0x80000000) ; | ||||
|     UART0_REG(UART_REG_TXFIFO) = *s; | ||||
|  | ||||
|     if (*s == '\n') { | ||||
|       while (UART0_REG(UART_REG_TXFIFO) & 0x80000000) ; | ||||
|       UART0_REG(UART_REG_TXFIFO) = '\r'; | ||||
|     } | ||||
|  | ||||
|     ++s; | ||||
|   } | ||||
|  | ||||
|   return 0; | ||||
| } | ||||
| weak_under_alias(puts); | ||||
| @@ -7,12 +7,13 @@ | ||||
|  | ||||
| #include "platform.h" | ||||
| #include "stub.h" | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| ssize_t __wrap_read(int fd, void* ptr, size_t len) | ||||
| { | ||||
|   uint8_t * current = (uint8_t *)ptr; | ||||
|   volatile uint32_t * uart_rx = (uint32_t *)(UART0_BASE_ADDR + UART_REG_RXFIFO); | ||||
|   volatile uint8_t * uart_rx_cnt = (uint8_t *)(UART0_BASE_ADDR + UART_REG_RXCTRL + 2); | ||||
|   volatile uint32_t * uart_rx = (uint32_t *)(UART0_CTRL_ADDR + UART_REG_RXFIFO); | ||||
|   volatile uint8_t * uart_rx_cnt = (uint8_t *)(UART0_CTRL_ADDR + UART_REG_RXCTRL + 2); | ||||
|  | ||||
|   ssize_t result = 0; | ||||
|  | ||||
| @@ -28,3 +29,4 @@ ssize_t __wrap_read(int fd, void* ptr, size_t len) | ||||
|  | ||||
|   return _stub(EBADF); | ||||
| } | ||||
| weak_under_alias(read); | ||||
|   | ||||
| @@ -1,6 +1,7 @@ | ||||
| /* See LICENSE of license details. */ | ||||
|  | ||||
| #include <stddef.h> | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| void *__wrap_sbrk(ptrdiff_t incr) | ||||
| { | ||||
| @@ -14,3 +15,4 @@ void *__wrap_sbrk(ptrdiff_t incr) | ||||
|   curbrk += incr; | ||||
|   return curbrk - incr; | ||||
| } | ||||
| weak_under_alias(sbrk); | ||||
|   | ||||
| @@ -3,8 +3,10 @@ | ||||
| #include <errno.h> | ||||
| #include <sys/stat.h> | ||||
| #include "stub.h" | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| int __wrap_stat(const char* file, struct stat* st) | ||||
| { | ||||
|   return _stub(EACCES); | ||||
| } | ||||
| weak_under_alias(stat); | ||||
|   | ||||
| @@ -3,8 +3,10 @@ | ||||
| #include <errno.h> | ||||
| #include <sys/times.h> | ||||
| #include "stub.h" | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| clock_t __wrap_times(struct tms* buf) | ||||
| { | ||||
|   return _stub(EACCES); | ||||
| } | ||||
| weak_under_alias(times); | ||||
|   | ||||
| @@ -2,8 +2,10 @@ | ||||
|  | ||||
| #include <errno.h> | ||||
| #include "stub.h" | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| int __wrap_unlink(const char* name) | ||||
| { | ||||
|   return _stub(ENOENT); | ||||
| } | ||||
| weak_under_alias(unlink); | ||||
|   | ||||
							
								
								
									
										7
									
								
								hello-world/bsp/libwrap/sys/weak_under_alias.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								hello-world/bsp/libwrap/sys/weak_under_alias.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| #ifndef _BSP_LIBWRAP_WEAK_UNDER_ALIAS_H | ||||
| #define _BSP_LIBWRAP_WEAK_UNDER_ALIAS_H | ||||
|  | ||||
| #define weak_under_alias(name) \ | ||||
|   extern __typeof (__wrap_##name) __wrap__##name __attribute__ ((weak, alias ("__wrap_"#name))) | ||||
|  | ||||
| #endif | ||||
| @@ -7,6 +7,7 @@ | ||||
|  | ||||
| #include "platform.h" | ||||
| #include "stub.h" | ||||
| #include "weak_under_alias.h" | ||||
|  | ||||
| ssize_t __wrap_write(int fd, const void* ptr, size_t len) | ||||
| { | ||||
| @@ -27,3 +28,4 @@ ssize_t __wrap_write(int fd, const void* ptr, size_t len) | ||||
|  | ||||
|   return _stub(EBADF); | ||||
| } | ||||
| weak_under_alias(write); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user