Browse Source

Fixes to build kernel and modules with clang

K. Lange 2 years ago
parent
commit
31214693dd

+ 1 - 1
base/usr/include/kernel/libc.h

@@ -2,7 +2,7 @@
  */
 #pragma once
 
-#include <stddef.h>
+#include <kernel/types.h>
 
 #define MIN(A, B) ((A) < (B) ? (A) : (B))
 #define MAX(A, B) ((A) > (B) ? (A) : (B))

+ 4 - 4
base/usr/include/kernel/system.h

@@ -159,10 +159,10 @@ uintptr_t memory_use(void);
 uintptr_t memory_total(void);
 
 /* klmalloc */
-void * __attribute__ ((malloc)) malloc(size_t size);
-void * __attribute__ ((malloc)) realloc(void *ptr, size_t size);
-void * __attribute__ ((malloc)) calloc(size_t nmemb, size_t size);
-void * __attribute__ ((malloc)) valloc(size_t size);
+void * __attribute__ ((malloc)) malloc(uintptr_t size);
+void * __attribute__ ((malloc)) realloc(void *ptr, uintptr_t size);
+void * __attribute__ ((malloc)) calloc(uintptr_t nmemb, uintptr_t size);
+void * __attribute__ ((malloc)) valloc(uintptr_t size);
 void free(void *ptr);
 
 /* Tasks */

+ 1 - 0
base/usr/include/kernel/types.h

@@ -8,6 +8,7 @@
 
 #include <stdint.h>
 
+typedef int wchar_t;
 typedef unsigned long size_t;
 #define CHAR_BIT 8
 

+ 4 - 1
kernel/libc.c

@@ -8,7 +8,10 @@
  */
 
 #include <kernel/system.h>
-#include <limits.h>
+
+#ifndef UCHAR_MAX
+#define UCHAR_MAX 255
+#endif
 
 #define ALIGN (sizeof(size_t))
 #define ONES ((size_t)-1/UCHAR_MAX)

+ 1 - 1
kernel/misc/elf.c

@@ -219,7 +219,7 @@ int exec_shebang(char * path, fs_node_t * file, int argc, char ** argv, char **
 	memcpy(script, path, strlen(path)+1);
 
 	unsigned int nargc = argc + (arg ? 2 : 1);
-	char * args[nargc + 1];
+	char * args[nargc + 2];
 	args[0] = cmd;
 	args[1] = arg ? arg : script;
 	args[2] = arg ? script : NULL;

+ 39 - 2
kernel/sys/module.c

@@ -215,8 +215,39 @@ void * module_load_direct(void * blob, size_t length) {
 							uintptr_t final = s->sh_addr + table->st_value;
 							hashmap_set(symboltable, name, (void *)final);
 							hashmap_set(local_symbols, name, (void *)final);
+						} else {
+							debug_print(ERROR, "Not resolving %s", name);
 						}
 					}
+				} else if (ELF32_ST_BIND(table->st_info) == STB_LOCAL) {
+					char * name = (char *)((uintptr_t)symstrtab + table->st_name);
+					Elf32_Shdr * s = NULL;
+					{
+						int i = 0;
+						int set = 0;
+						for (unsigned int x = 0; x < (unsigned int)target->e_shentsize * target->e_shnum; x += target->e_shentsize) {
+							Elf32_Shdr * shdr = (Elf32_Shdr *)((uintptr_t)target + (target->e_shoff + x));
+							if (i == table->st_shndx) {
+								set = 1;
+								s = shdr;
+								break;
+							}
+							i++;
+						}
+						if (!set && table->st_shndx == 65522) {
+							if (!hashmap_get(symboltable, name)) {
+								void * final = calloc(1, table->st_value);
+								debug_print(NOTICE, "point %s to 0x%x", name, (uintptr_t)final);
+								hashmap_set(local_symbols, name, (void *)final);
+							}
+						}
+					}
+					if (s) {
+						uintptr_t final = s->sh_addr + table->st_value;
+						hashmap_set(local_symbols, name, (void *)final);
+					} else {
+						debug_print(ERROR, "Not resolving %s", name);
+					}
 				}
 			}
 			table++;
@@ -256,9 +287,15 @@ void * module_load_direct(void * blob, size_t length) {
 						addend = *ptr;
 						place  = (uintptr_t)ptr;
 						if (!hashmap_get(symboltable, name)) {
-							debug_print(ERROR, "Wat? Missing symbol %s", name);
+							if (!hashmap_get(local_symbols, name)) {
+								debug_print(ERROR, "Wat? Missing symbol %s", name);
+								debug_print(ERROR, "Here's all the symbols:");
+							} else {
+								symbol = (uintptr_t)hashmap_get(local_symbols, name);
+							}
+						} else {
+							symbol = (uintptr_t)hashmap_get(symboltable, name);
 						}
-						symbol = (uintptr_t)hashmap_get(symboltable, name);
 					}
 					switch (ELF32_R_TYPE(table->r_info)) {
 						case 1:

+ 1 - 0
modules/vgalog.c

@@ -2,6 +2,7 @@
 #include <kernel/printf.h>
 #include <kernel/module.h>
 #include <kernel/logging.h>
+#include <kernel/types.h>
 
 #include "../lib/termemu.c"