merge elf.h & def.h

master
WangRunji 6 years ago
parent 78bfb1f6ea
commit 10b2aa8b6d

@ -1,79 +0,0 @@
#ifndef __LIBS_DEFS_H__
#define __LIBS_DEFS_H__
#ifndef NULL
#define NULL ((void *)0)
#endif
#define __always_inline inline __attribute__((always_inline))
#define __noinline __attribute__((noinline))
#define __noreturn __attribute__((noreturn))
#define CHAR_BIT 8
/* Represents true-or-false values */
typedef long long bool;
/* Explicitly-sized versions of integer types */
typedef char int8_t;
typedef unsigned char uint8_t;
typedef short int16_t;
typedef unsigned short uint16_t;
typedef int int32_t;
typedef unsigned int uint32_t;
typedef long long int64_t;
typedef unsigned long long uint64_t;
/* *
* Pointers and addresses are 32 bits long.
* We use pointer types to represent addresses,
* uintptr_t to represent the numerical values of addresses.
* */
typedef int64_t intptr_t;
typedef uint64_t uintptr_t;
/* size_t is used for memory object sizes */
typedef uintptr_t size_t;
/* off_t is used for file offsets and lengths */
typedef intptr_t off_t;
/* used for page numbers */
typedef size_t ppn_t;
/* *
* Rounding operations (efficient when n is a power of 2)
* Round down to the nearest multiple of n
* */
#define ROUNDDOWN(a, n) ({ \
size_t __a = (size_t)(a); \
(typeof(a))(__a - __a % (n)); \
})
/* Round up to the nearest multiple of n */
#define ROUNDUP(a, n) ({ \
size_t __n = (size_t)(n); \
(typeof(a))(ROUNDDOWN((size_t)(a) + __n - 1, __n)); \
})
/* Round up the result of dividing of n */
#define ROUNDUP_DIV(a, n) ({ \
uint64_t __n = (uint64_t)(n); \
(typeof(a))(((a) + __n - 1) / __n); \
})
/* Return the offset of 'member' relative to the beginning of a struct type */
#define offsetof(type, member) \
((size_t)(&((type *)0)->member))
/* *
* to_struct - get the struct from a ptr
* @ptr: a struct pointer of member
* @type: the type of the struct this is embedded in
* @member: the name of the member within the struct
* */
#define to_struct(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))
#endif /* !__LIBS_DEFS_H__ */

@ -1,48 +0,0 @@
#ifndef __LIBS_ELF_H__
#define __LIBS_ELF_H__
#include <defs.h>
#define ELF_MAGIC 0x464C457FU // "\x7FELF" in little endian
/* file header */
struct elfhdr {
uint32_t e_magic; // must equal ELF_MAGIC
uint8_t e_elf[12];
uint16_t e_type; // 1=relocatable, 2=executable, 3=shared object, 4=core image
uint16_t e_machine; // 3=x86, 4=68K, etc.
uint32_t e_version; // file version, always 1
uint64_t e_entry; // entry point if executable
uint64_t e_phoff; // file position of program header or 0
uint64_t e_shoff; // file position of section header or 0
uint32_t e_flags; // architecture-specific flags, usually 0
uint16_t e_ehsize; // size of this elf header
uint16_t e_phentsize; // size of an entry in program header
uint16_t e_phnum; // number of entries in program header or 0
uint16_t e_shentsize; // size of an entry in section header
uint16_t e_shnum; // number of entries in section header or 0
uint16_t e_shstrndx; // section number that contains section name strings
};
/* program section header */
struct proghdr {
uint32_t p_type; // loadable code or data, dynamic linking info,etc.
uint32_t p_flags; // read/write/execute bits
uint64_t p_offset; // file offset of segment
uint64_t p_va; // virtual address to map segment
uint64_t p_pa; // physical address, not used
uint64_t p_filesz; // size of segment in file
uint64_t p_memsz; // size of segment in memory (bigger if contains bss
uint64_t p_align; // required alignment, invariably hardware page size
};
/* values for Proghdr::p_type */
#define ELF_PT_LOAD 1
/* flag bits for Proghdr::p_flags */
#define ELF_PF_X 1
#define ELF_PF_W 2
#define ELF_PF_R 4
#endif /* !__LIBS_ELF_H__ */

@ -1,79 +0,0 @@
#ifndef __LIBS_DEFS_H__
#define __LIBS_DEFS_H__
#ifndef NULL
#define NULL ((void *)0)
#endif
#define __always_inline inline __attribute__((always_inline))
#define __noinline __attribute__((noinline))
#define __noreturn __attribute__((noreturn))
#define CHAR_BIT 8
/* Represents true-or-false values */
typedef int bool;
/* Explicitly-sized versions of integer types */
typedef char int8_t;
typedef unsigned char uint8_t;
typedef short int16_t;
typedef unsigned short uint16_t;
typedef int int32_t;
typedef unsigned int uint32_t;
typedef long long int64_t;
typedef unsigned long long uint64_t;
/* *
* Pointers and addresses are 32 bits long.
* We use pointer types to represent addresses,
* uintptr_t to represent the numerical values of addresses.
* */
typedef int32_t intptr_t;
typedef uint32_t uintptr_t;
/* size_t is used for memory object sizes */
typedef uintptr_t size_t;
/* off_t is used for file offsets and lengths */
typedef intptr_t off_t;
/* used for page numbers */
typedef size_t ppn_t;
/* *
* Rounding operations (efficient when n is a power of 2)
* Round down to the nearest multiple of n
* */
#define ROUNDDOWN(a, n) ({ \
size_t __a = (size_t)(a); \
(typeof(a))(__a - __a % (n)); \
})
/* Round up to the nearest multiple of n */
#define ROUNDUP(a, n) ({ \
size_t __n = (size_t)(n); \
(typeof(a))(ROUNDDOWN((size_t)(a) + __n - 1, __n)); \
})
/* Round up the result of dividing of n */
#define ROUNDUP_DIV(a, n) ({ \
uint32_t __n = (uint32_t)(n); \
(typeof(a))(((a) + __n - 1) / __n); \
})
/* Return the offset of 'member' relative to the beginning of a struct type */
#define offsetof(type, member) \
((size_t)(&((type *)0)->member))
/* *
* to_struct - get the struct from a ptr
* @ptr: a struct pointer of member
* @type: the type of the struct this is embedded in
* @member: the name of the member within the struct
* */
#define to_struct(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))
#endif /* !__LIBS_DEFS_H__ */

@ -1,48 +0,0 @@
#ifndef __LIBS_ELF_H__
#define __LIBS_ELF_H__
#include <defs.h>
#define ELF_MAGIC 0x464C457FU // "\x7FELF" in little endian
/* file header */
struct elfhdr {
uint32_t e_magic; // must equal ELF_MAGIC
uint8_t e_elf[12];
uint16_t e_type; // 1=relocatable, 2=executable, 3=shared object, 4=core image
uint16_t e_machine; // 3=x86, 4=68K, etc.
uint32_t e_version; // file version, always 1
uint32_t e_entry; // entry point if executable
uint32_t e_phoff; // file position of program header or 0
uint32_t e_shoff; // file position of section header or 0
uint32_t e_flags; // architecture-specific flags, usually 0
uint16_t e_ehsize; // size of this elf header
uint16_t e_phentsize; // size of an entry in program header
uint16_t e_phnum; // number of entries in program header or 0
uint16_t e_shentsize; // size of an entry in section header
uint16_t e_shnum; // number of entries in section header or 0
uint16_t e_shstrndx; // section number that contains section name strings
};
/* program section header */
struct proghdr {
uint32_t p_type; // loadable code or data, dynamic linking info,etc.
uint32_t p_offset; // file offset of segment
uint32_t p_va; // virtual address to map segment
uint32_t p_pa; // physical address, not used
uint32_t p_filesz; // size of segment in file
uint32_t p_memsz; // size of segment in memory (bigger if contains bss
uint32_t p_flags; // read/write/execute bits
uint32_t p_align; // required alignment, invariably hardware page size
};
/* values for Proghdr::p_type */
#define ELF_PT_LOAD 1
/* flag bits for Proghdr::p_flags */
#define ELF_PF_X 1
#define ELF_PF_W 2
#define ELF_PF_R 4
#endif /* !__LIBS_ELF_H__ */

@ -5,7 +5,7 @@
int int
main(void) { main(void) {
#if defined(__x86_64__) || defined(__x86__) #if defined(__x86_64__) || defined(__i386__)
asm volatile("movw $0x28,%ax; movw %ax,%ds"); asm volatile("movw $0x28,%ax; movw %ax,%ds");
panic("FAIL: T.T\n"); panic("FAIL: T.T\n");
#endif #endif

@ -11,6 +11,9 @@
#define CHAR_BIT 8 #define CHAR_BIT 8
/* Represents true-or-false values */
typedef int bool;
/* Explicitly-sized versions of integer types */ /* Explicitly-sized versions of integer types */
typedef char int8_t; typedef char int8_t;
typedef unsigned char uint8_t; typedef unsigned char uint8_t;
@ -26,17 +29,14 @@ typedef unsigned long long uint64_t;
* We use pointer types to represent addresses, * We use pointer types to represent addresses,
* uintptr_t to represent the numerical values of addresses. * uintptr_t to represent the numerical values of addresses.
* */ * */
#if __riscv_xlen == 64 #if __riscv_xlen == 64 || defined(__x86_64__) || defined(__aarch64__)
typedef int64_t intptr_t; typedef int64_t intptr_t;
typedef uint64_t uintptr_t; typedef uint64_t uintptr_t;
#elif __riscv_xlen == 32 #elif __riscv_xlen == 32 || defined(__i386__)
typedef int32_t intptr_t; typedef int32_t intptr_t;
typedef uint32_t uintptr_t; typedef uint32_t uintptr_t;
#endif #endif
/* Represents true-or-false values */
typedef intptr_t bool;
/* size_t is used for memory object sizes */ /* size_t is used for memory object sizes */
typedef uintptr_t size_t; typedef uintptr_t size_t;

@ -25,7 +25,7 @@ struct elfhdr {
}; };
/* program section header */ /* program section header */
#if __riscv_xlen == 64 #if __riscv_xlen == 64 || defined(__x86_64__) || defined(__aarch64__)
struct proghdr { struct proghdr {
uint32_t p_type; // loadable code or data, dynamic linking info,etc. uint32_t p_type; // loadable code or data, dynamic linking info,etc.
uint32_t p_flags; // read/write/execute bits uint32_t p_flags; // read/write/execute bits
@ -36,7 +36,7 @@ struct proghdr {
uint64_t p_memsz; // size of segment in memory (bigger if contains bss uint64_t p_memsz; // size of segment in memory (bigger if contains bss
uint64_t p_align; // required alignment, invariably hardware page size uint64_t p_align; // required alignment, invariably hardware page size
}; };
#elif __riscv_xlen == 32 #elif __riscv_xlen == 32 || defined(__i386__)
struct proghdr { struct proghdr {
uint32_t p_type; // loadable code or data, dynamic linking info,etc. uint32_t p_type; // loadable code or data, dynamic linking info,etc.
uint32_t p_offset; // file offset of segment uint32_t p_offset; // file offset of segment

@ -3,7 +3,7 @@
int int
main(void) { main(void) {
#if defined(__x86_64__) || defined(__x86__) #if defined(__x86_64__) || defined(__i386__)
asm volatile("int $14"); asm volatile("int $14");
panic("FAIL: T.T\n"); panic("FAIL: T.T\n");
#endif #endif

Loading…
Cancel
Save