summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--AK/Assertions.h2
-rw-r--r--AK/Compiler.h9
-rw-r--r--AK/RetainPtr.h1
-rw-r--r--AK/kmalloc.cpp14
-rw-r--r--AK/kmalloc.h6
-rw-r--r--ELFLoader/ELFImage.h18
-rw-r--r--ELFLoader/ELFLoader.cpp39
-rw-r--r--ELFLoader/ELFLoader.h4
-rw-r--r--Kernel/Task.cpp1
-rw-r--r--Kernel/_fs_contentsbin1024000 -> 1024000 bytes
-rw-r--r--Kernel/kprintf.h2
-rw-r--r--LibC/types.h2
-rw-r--r--LibC/unistd.cpp4
-rw-r--r--Userland/Makefile2
14 files changed, 80 insertions, 24 deletions
diff --git a/AK/Assertions.h b/AK/Assertions.h
index a941d7a697..9e95bcc6c2 100644
--- a/AK/Assertions.h
+++ b/AK/Assertions.h
@@ -1,7 +1,7 @@
#pragma once
#ifdef SERENITY
-#include "kassert.h"
+#include <Kernel/kassert.h>
#else
#include <assert.h>
#define ASSERT(x) assert(x)
diff --git a/AK/Compiler.h b/AK/Compiler.h
new file mode 100644
index 0000000000..0ae775e888
--- /dev/null
+++ b/AK/Compiler.h
@@ -0,0 +1,9 @@
+#pragma once
+
+#define PACKED __attribute__ ((packed))
+#define NORETURN __attribute__ ((noreturn))
+#define ALWAYS_INLINE __attribute__ ((always_inline))
+#define NEVER_INLINE __attribute__ ((noinline))
+#define MALLOC_ATTR __attribute__ ((malloc))
+#define PURE __attribute__ ((pure))
+
diff --git a/AK/RetainPtr.h b/AK/RetainPtr.h
index c2fbd215d0..d234b45e91 100644
--- a/AK/RetainPtr.h
+++ b/AK/RetainPtr.h
@@ -1,5 +1,6 @@
#pragma once
+#include "Compiler.h"
#include "Types.h"
namespace AK {
diff --git a/AK/kmalloc.cpp b/AK/kmalloc.cpp
index 448b2ca6db..80b662304f 100644
--- a/AK/kmalloc.cpp
+++ b/AK/kmalloc.cpp
@@ -1,9 +1,19 @@
-#include <cstdio>
-#include "SimpleMalloc.h"
#include "kmalloc.h"
+
+#ifndef SERENITY
+#include <cstdio>
#include <cstdlib>
+#endif
+#if defined(SERENITY) && defined(USERLAND)
#define USE_SYSTEM_MALLOC
+#endif
+
+#define USE_SYSTEM_MALLOC
+
+#ifndef USE_SYSTEM_MALLOC
+#include "SimpleMalloc.h"
+#endif
#ifdef USE_SYSTEM_MALLOC
diff --git a/AK/kmalloc.h b/AK/kmalloc.h
index 2c5c2bdb4f..24f48f376f 100644
--- a/AK/kmalloc.h
+++ b/AK/kmalloc.h
@@ -1,7 +1,11 @@
#pragma once
#ifdef SERENITY
+#ifdef USERLAND
+#include <LibC/stdlib.h>
+#else
#include <Kernel/kmalloc.h>
+#endif
#else
#include <new>
@@ -10,7 +14,7 @@
extern "C" {
void* kcalloc(size_t nmemb, size_t size);
-void* kmalloc(size_t size) __attribute__ ((malloc));
+void* kmalloc(size_t size) MALLOC_ATTR;
void kfree(void* ptr);
void* krealloc(void* ptr, size_t size);
diff --git a/ELFLoader/ELFImage.h b/ELFLoader/ELFImage.h
index 839fa20c0b..ff0715ee21 100644
--- a/ELFLoader/ELFImage.h
+++ b/ELFLoader/ELFImage.h
@@ -147,22 +147,28 @@ inline void ELFImage::forEachSectionOfType(unsigned type, F func) const
{
for (unsigned i = 0; i < sectionCount(); ++i) {
auto& section = this->section(i);
- if (section.type() == type)
- func(section);
+ if (section.type() == type) {
+ if (!func(section))
+ break;
+ }
}
}
template<typename F>
inline void ELFImage::RelocationSection::forEachRelocation(F func) const
{
- for (unsigned i = 0; i < relocationCount(); ++i)
- func(relocation(i));
+ for (unsigned i = 0; i < relocationCount(); ++i) {
+ if (!func(relocation(i)))
+ break;
+ }
}
template<typename F>
inline void ELFImage::forEachSymbol(F func) const
{
- for (unsigned i = 0; i < symbolCount(); ++i)
- func(symbol(i));
+ for (unsigned i = 0; i < symbolCount(); ++i) {
+ if (!func(symbol(i)))
+ break;
+ }
}
diff --git a/ELFLoader/ELFLoader.cpp b/ELFLoader/ELFLoader.cpp
index 3b3bf4845e..a14b3443f7 100644
--- a/ELFLoader/ELFLoader.cpp
+++ b/ELFLoader/ELFLoader.cpp
@@ -25,26 +25,38 @@ bool ELFLoader::load()
if (!m_image->isValid())
return false;
- layout();
+ if (!layout())
+ return false;
exportSymbols();
- performRelocations();
+ if (!performRelocations())
+ return false;
return true;
}
-void ELFLoader::layout()
+bool ELFLoader::layout()
{
#ifdef ELFLOADER_DEBUG
kprintf("[ELFLoader] Layout\n");
#endif
- m_image->forEachSectionOfType(SHT_PROGBITS, [this] (const ELFImage::Section& section) {
+ bool failed = false;
+ m_image->forEachSectionOfType(SHT_PROGBITS, [this, &failed] (const ELFImage::Section& section) {
#ifdef ELFLOADER_DEBUG
kprintf("[ELFLoader] Allocating progbits section: %s\n", section.name());
#endif
+ if (!section.size())
+ return true;
char* ptr = m_execSpace.allocateArea(section.name(), section.size());
+ if (!ptr) {
+ kprintf("ELFLoader: failed to allocate section '%s'\n", section.name());
+ failed = true;
+ return false;
+ }
memcpy(ptr, section.rawData(), section.size());
m_sections.set(section.name(), move(ptr));
+ return true;
});
+ return !failed;
}
void* ELFLoader::lookup(const ELFImage::Symbol& symbol)
@@ -67,23 +79,30 @@ char* ELFLoader::areaForSectionName(const char* name)
return nullptr;
}
-void ELFLoader::performRelocations()
+bool ELFLoader::performRelocations()
{
#ifdef ELFLOADER_DEBUG
kprintf("[ELFLoader] Performing relocations\n");
#endif
- m_image->forEachSectionOfType(SHT_PROGBITS, [this] (const ELFImage::Section& section) {
+ bool failed = false;
+
+ m_image->forEachSectionOfType(SHT_PROGBITS, [this, &failed] (const ELFImage::Section& section) -> bool {
auto& relocations = section.relocations();
if (relocations.isUndefined())
- return;
- relocations.forEachRelocation([this, section] (const ELFImage::Relocation& relocation) {
+ return true;
+ relocations.forEachRelocation([this, section, &failed] (const ELFImage::Relocation& relocation) {
auto symbol = relocation.symbol();
auto& patchPtr = *reinterpret_cast<ptrdiff_t*>(areaForSection(section) + relocation.offset());
switch (relocation.type()) {
case R_386_PC32: {
char* targetPtr = (char*)lookup(symbol);
+ if (!targetPtr) {
+ kprintf("ELFLoader: unresolved symbol '%s'\n", symbol.name());
+ failed = true;
+ return false;
+ }
ptrdiff_t relativeOffset = (char*)targetPtr - ((char*)&patchPtr + 4);
#ifdef ELFLOADER_DEBUG
kprintf("[ELFLoader] Relocate PC32: offset=%x, symbol=%u(%s) value=%x target=%p, offset=%d\n",
@@ -115,8 +134,11 @@ void ELFLoader::performRelocations()
ASSERT_NOT_REACHED();
break;
}
+ return true;
});
+ return !failed;
});
+ return !failed;
}
void ELFLoader::exportSymbols()
@@ -128,6 +150,7 @@ void ELFLoader::exportSymbols()
if (symbol.type() == STT_FUNC)
m_execSpace.addSymbol(symbol.name(), areaForSection(symbol.section()) + symbol.value(), symbol.size());
// FIXME: What about other symbol types?
+ return true;
});
}
diff --git a/ELFLoader/ELFLoader.h b/ELFLoader/ELFLoader.h
index f8f4877243..b7a1f5161f 100644
--- a/ELFLoader/ELFLoader.h
+++ b/ELFLoader/ELFLoader.h
@@ -19,8 +19,8 @@ public:
bool load();
private:
- void layout();
- void performRelocations();
+ bool layout();
+ bool performRelocations();
void exportSymbols();
void* lookup(const ELFImage::Symbol&);
char* areaForSection(const ELFImage::Section&);
diff --git a/Kernel/Task.cpp b/Kernel/Task.cpp
index 50bc885708..0ef0831070 100644
--- a/Kernel/Task.cpp
+++ b/Kernel/Task.cpp
@@ -201,6 +201,7 @@ Task* Task::create(const String& path, uid_t uid, gid_t gid, pid_t parentPID)
bool success = space.loadELF(move(elfData));
if (!success) {
delete t;
+ kprintf("Failure loading ELF %s\n", path.characters());
return nullptr;
}
diff --git a/Kernel/_fs_contents b/Kernel/_fs_contents
index fbf9da7dbd..d3cf094bfb 100644
--- a/Kernel/_fs_contents
+++ b/Kernel/_fs_contents
Binary files differ
diff --git a/Kernel/kprintf.h b/Kernel/kprintf.h
index bf7e74c2cf..d35d9d492a 100644
--- a/Kernel/kprintf.h
+++ b/Kernel/kprintf.h
@@ -1,5 +1,7 @@
#pragma once
+#include <AK/Compiler.h>
+
int kprintf(const char *fmt, ...);
int ksprintf(char* buf, const char *fmt, ...);
diff --git a/LibC/types.h b/LibC/types.h
index d28920f7f2..23f532c938 100644
--- a/LibC/types.h
+++ b/LibC/types.h
@@ -12,7 +12,7 @@ typedef signed char signed_byte;
typedef dword uid_t;
typedef dword gid_t;
-typedef dword pid_t;
+typedef int pid_t;
typedef dword size_t;
typedef signed_dword ssize_t;
diff --git a/LibC/unistd.cpp b/LibC/unistd.cpp
index f0c5831940..814346e075 100644
--- a/LibC/unistd.cpp
+++ b/LibC/unistd.cpp
@@ -9,12 +9,12 @@ uid_t getuid()
return Syscall::invoke(Syscall::PosixGetuid);
}
-uid_t getgid()
+gid_t getgid()
{
return Syscall::invoke(Syscall::PosixGetgid);
}
-uid_t getpid()
+pid_t getpid()
{
return Syscall::invoke(Syscall::PosixGetpid);
}
diff --git a/Userland/Makefile b/Userland/Makefile
index e47a8578b6..2a866fcc04 100644
--- a/Userland/Makefile
+++ b/Userland/Makefile
@@ -20,7 +20,7 @@ FLAVOR_FLAGS = -fomit-frame-pointer -mregparm=3 -march=i386 -m32 -fno-exceptions
OPTIMIZATION_FLAGS = -Os -fno-asynchronous-unwind-tables
INCLUDE_FLAGS = -I.. -I.
-DEFINES = -DSERENITY -DSANITIZE_PTRS
+DEFINES = -DSERENITY -DSANITIZE_PTRS -DUSERLAND
CXXFLAGS = $(WARNING_FLAGS) $(OPTIMIZATION_FLAGS) $(USERLAND_FLAGS) $(FLAVOR_FLAGS) $(ARCH_FLAGS) $(STANDARD_FLAGS) $(INCLUDE_FLAGS) $(DEFINES)
CXX = g++