From 95a5f8296188d7dd8ffb86afd4f0c3280e2f5656 Mon Sep 17 00:00:00 2001 From: Jacek Caban Date: Mon, 16 Mar 2020 16:05:39 +0100 Subject: [PATCH] dbghelp: Use vtbl to handle different image_file_map types. Signed-off-by: Jacek Caban Signed-off-by: Alexandre Julliard --- dlls/dbghelp/elf_module.c | 44 +++++++------------- dlls/dbghelp/image_private.h | 79 +++++++----------------------------- dlls/dbghelp/macho_module.c | 41 ++++++++----------- dlls/dbghelp/pe_module.c | 24 +++++++---- 4 files changed, 62 insertions(+), 126 deletions(-) diff --git a/dlls/dbghelp/elf_module.c b/dlls/dbghelp/elf_module.c index a5b7c1dc5ca..300926f88b9 100644 --- a/dlls/dbghelp/elf_module.c +++ b/dlls/dbghelp/elf_module.c @@ -134,7 +134,7 @@ struct elf_module_info * * Maps a single section into memory from an ELF file */ -const char* elf_map_section(struct image_section_map* ism) +static const char* elf_map_section(struct image_section_map* ism) { struct elf_file_map* fmap = &ism->fmap->u.elf; SYSTEM_INFO sysinfo; @@ -176,7 +176,7 @@ const char* elf_map_section(struct image_section_map* ism) * Finds a section by name (and type) into memory from an ELF file * or its alternate if any */ -BOOL elf_find_section(struct image_file_map* _fmap, const char* name, struct image_section_map* ism) +static BOOL elf_find_section(struct image_file_map* _fmap, const char* name, struct image_section_map* ism) { struct elf_file_map* fmap = &_fmap->u.elf; unsigned i; @@ -233,7 +233,7 @@ static BOOL elf_find_section_type(struct image_file_map* _fmap, const char* name * * Unmaps a single section from memory */ -void elf_unmap_section(struct image_section_map* ism) +static void elf_unmap_section(struct image_section_map* ism) { struct elf_file_map* fmap = &ism->fmap->u.elf; @@ -265,7 +265,7 @@ static void elf_end_find(struct image_file_map* fmap) * * Get the RVA of an ELF section */ -DWORD_PTR elf_get_map_rva(const struct image_section_map* ism) +static DWORD_PTR elf_get_map_rva(const struct image_section_map* ism) { if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum) return 0; @@ -277,15 +277,25 @@ DWORD_PTR elf_get_map_rva(const struct image_section_map* ism) * * Get the size of an ELF section */ -unsigned elf_get_map_size(const struct image_section_map* ism) +static unsigned elf_get_map_size(const struct image_section_map* ism) { if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.elf.elfhdr.e_shnum) return 0; return ism->fmap->u.elf.sect[ism->sidx].shdr.sh_size; } +static const struct image_file_map_ops elf_file_map_ops = +{ + elf_map_section, + elf_unmap_section, + elf_find_section, + elf_get_map_rva, + elf_get_map_size, +}; + static inline void elf_reset_file_map(struct image_file_map* fmap) { + fmap->ops = &elf_file_map_ops; fmap->alternate = NULL; fmap->u.elf.handle = INVALID_HANDLE_VALUE; fmap->u.elf.shstrtab = IMAGE_NO_MAP; @@ -2036,30 +2046,6 @@ BOOL elf_synchronize_module_list(struct process* pcs) #else /* !__ELF__ */ -BOOL elf_find_section(struct image_file_map* fmap, const char* name, - struct image_section_map* ism) -{ - return FALSE; -} - -const char* elf_map_section(struct image_section_map* ism) -{ - return NULL; -} - -void elf_unmap_section(struct image_section_map* ism) -{} - -unsigned elf_get_map_size(const struct image_section_map* ism) -{ - return 0; -} - -DWORD_PTR elf_get_map_rva(const struct image_section_map* ism) -{ - return 0; -} - BOOL elf_synchronize_module_list(struct process* pcs) { return FALSE; diff --git a/dlls/dbghelp/image_private.h b/dlls/dbghelp/image_private.h index 8dead8048c3..e0790b4c7b2 100644 --- a/dlls/dbghelp/image_private.h +++ b/dlls/dbghelp/image_private.h @@ -60,6 +60,7 @@ struct image_file_map { enum module_type modtype; + const struct image_file_map_ops *ops; unsigned addr_size; /* either 16 (not used), 32 or 64 */ struct image_file_map* alternate; /* another file linked to this one */ union @@ -129,45 +130,21 @@ struct image_section_map long sidx; }; -extern BOOL elf_find_section(struct image_file_map* fmap, const char* name, - struct image_section_map* ism) DECLSPEC_HIDDEN; -extern const char* elf_map_section(struct image_section_map* ism) DECLSPEC_HIDDEN; -extern void elf_unmap_section(struct image_section_map* ism) DECLSPEC_HIDDEN; -extern DWORD_PTR elf_get_map_rva(const struct image_section_map* ism) DECLSPEC_HIDDEN; -extern unsigned elf_get_map_size(const struct image_section_map* ism) DECLSPEC_HIDDEN; - -extern BOOL macho_find_section(struct image_file_map* ifm, const char* segname, - const char* sectname, struct image_section_map* ism) DECLSPEC_HIDDEN; -extern const char* macho_map_section(struct image_section_map* ism) DECLSPEC_HIDDEN; -extern void macho_unmap_section(struct image_section_map* ism) DECLSPEC_HIDDEN; -extern DWORD_PTR macho_get_map_rva(const struct image_section_map* ism) DECLSPEC_HIDDEN; -extern unsigned macho_get_map_size(const struct image_section_map* ism) DECLSPEC_HIDDEN; - -extern BOOL pe_find_section(struct image_file_map* fmap, const char* name, - struct image_section_map* ism) DECLSPEC_HIDDEN; -extern const char* pe_map_section(struct image_section_map* psm) DECLSPEC_HIDDEN; -extern void pe_unmap_section(struct image_section_map* psm) DECLSPEC_HIDDEN; -extern DWORD_PTR pe_get_map_rva(const struct image_section_map* psm) DECLSPEC_HIDDEN; -extern unsigned pe_get_map_size(const struct image_section_map* psm) DECLSPEC_HIDDEN; +struct image_file_map_ops +{ + const char* (*map_section)(struct image_section_map* ism); + void (*unmap_section)(struct image_section_map* ism); + BOOL (*find_section)(struct image_file_map* fmap, const char* name, struct image_section_map* ism); + DWORD_PTR (*get_map_rva)(const struct image_section_map* ism); + unsigned (*get_map_size)(const struct image_section_map* ism); +}; static inline BOOL image_find_section(struct image_file_map* fmap, const char* name, struct image_section_map* ism) { while (fmap) { - switch (fmap->modtype) - { - case DMT_ELF: - if (elf_find_section(fmap, name, ism)) return TRUE; - break; - case DMT_MACHO: - if (macho_find_section(fmap, NULL, name, ism)) return TRUE; - break; - case DMT_PE: - if (pe_find_section(fmap, name, ism)) return TRUE; - break; - default: assert(0); return FALSE; - } + if (fmap->ops->find_section(fmap, name, ism)) return TRUE; fmap = fmap->alternate; } ism->fmap = NULL; @@ -177,48 +154,20 @@ static inline BOOL image_find_section(struct image_file_map* fmap, const char* n static inline const char* image_map_section(struct image_section_map* ism) { - if (!ism->fmap) return NULL; - switch (ism->fmap->modtype) - { - case DMT_ELF: return elf_map_section(ism); - case DMT_MACHO: return macho_map_section(ism); - case DMT_PE: return pe_map_section(ism); - default: assert(0); return NULL; - } + return ism->fmap ? ism->fmap->ops->map_section(ism) : NULL; } static inline void image_unmap_section(struct image_section_map* ism) { - if (!ism->fmap) return; - switch (ism->fmap->modtype) - { - case DMT_ELF: elf_unmap_section(ism); break; - case DMT_MACHO: macho_unmap_section(ism); break; - case DMT_PE: pe_unmap_section(ism); break; - default: assert(0); return; - } + if (ism->fmap) ism->fmap->ops->unmap_section(ism); } static inline DWORD_PTR image_get_map_rva(const struct image_section_map* ism) { - if (!ism->fmap) return 0; - switch (ism->fmap->modtype) - { - case DMT_ELF: return elf_get_map_rva(ism); - case DMT_MACHO: return macho_get_map_rva(ism); - case DMT_PE: return pe_get_map_rva(ism); - default: assert(0); return 0; - } + return ism->fmap ? ism->fmap->ops->get_map_rva(ism) : 0; } static inline unsigned image_get_map_size(const struct image_section_map* ism) { - if (!ism->fmap) return 0; - switch (ism->fmap->modtype) - { - case DMT_ELF: return elf_get_map_size(ism); - case DMT_MACHO: return macho_get_map_size(ism); - case DMT_PE: return pe_get_map_size(ism); - default: assert(0); return 0; - } + return ism->fmap ? ism->fmap->ops->get_map_size(ism) : 0; } diff --git a/dlls/dbghelp/macho_module.c b/dlls/dbghelp/macho_module.c index 1df48c8a951..a9a086ea767 100644 --- a/dlls/dbghelp/macho_module.c +++ b/dlls/dbghelp/macho_module.c @@ -332,7 +332,7 @@ static void macho_unmap_ranges(const struct macho_file_map* fmap, /****************************************************************** * macho_find_section */ -BOOL macho_find_section(struct image_file_map* ifm, const char* segname, const char* sectname, struct image_section_map* ism) +static BOOL macho_find_segment_section(struct image_file_map* ifm, const char* segname, const char* sectname, struct image_section_map* ism) { struct macho_file_map* fmap; unsigned i; @@ -369,6 +369,11 @@ BOOL macho_find_section(struct image_file_map* ifm, const char* segname, const c return FALSE; } +static BOOL macho_find_section(struct image_file_map* ifm, const char* sectname, struct image_section_map* ism) +{ + return macho_find_segment_section(ifm, NULL, sectname, ism); +} + /****************************************************************** * macho_map_section */ @@ -420,6 +425,15 @@ unsigned macho_get_map_size(const struct image_section_map* ism) return ism->fmap->u.macho.sect[ism->sidx].section.size; } +static const struct image_file_map_ops macho_file_map_ops = +{ + macho_map_section, + macho_unmap_section, + macho_find_section, + macho_get_map_rva, + macho_get_map_size, +}; + /****************************************************************** * macho_map_load_commands * @@ -680,6 +694,7 @@ static BOOL macho_map_file(struct process *pcs, const WCHAR *filenameW, reset_file_map(ifm); ifm->modtype = DMT_MACHO; + ifm->ops = &macho_file_map_ops; ifm->alternate = NULL; ifm->addr_size = (pcs->is_64bit) ? 64 : 32; fmap->header_size = (pcs->is_64bit) ? sizeof(struct mach_header_64) : sizeof(struct mach_header); @@ -1998,30 +2013,6 @@ struct module* macho_load_module(struct process* pcs, const WCHAR* name, unsign #else /* HAVE_MACH_O_LOADER_H */ -BOOL macho_find_section(struct image_file_map* ifm, const char* segname, const char* sectname, struct image_section_map* ism) -{ - return FALSE; -} - -const char* macho_map_section(struct image_section_map* ism) -{ - return NULL; -} - -void macho_unmap_section(struct image_section_map* ism) -{ -} - -DWORD_PTR macho_get_map_rva(const struct image_section_map* ism) -{ - return 0; -} - -unsigned macho_get_map_size(const struct image_section_map* ism) -{ - return 0; -} - BOOL macho_synchronize_module_list(struct process* pcs) { return FALSE; diff --git a/dlls/dbghelp/pe_module.c b/dlls/dbghelp/pe_module.c index a5b8353f3c2..a12a375ec54 100644 --- a/dlls/dbghelp/pe_module.c +++ b/dlls/dbghelp/pe_module.c @@ -73,7 +73,7 @@ static void pe_unmap_full(struct image_file_map* fmap) * * Maps a single section into memory from an PE file */ -const char* pe_map_section(struct image_section_map* ism) +static const char* pe_map_section(struct image_section_map* ism) { void* mapping; struct pe_file_map* fmap = &ism->fmap->u.pe; @@ -111,8 +111,8 @@ const char* pe_map_section(struct image_section_map* ism) * Finds a section by name (and type) into memory from an PE file * or its alternate if any */ -BOOL pe_find_section(struct image_file_map* fmap, const char* name, - struct image_section_map* ism) +static BOOL pe_find_section(struct image_file_map* fmap, const char* name, + struct image_section_map* ism) { const char* sectname; unsigned i; @@ -148,7 +148,7 @@ BOOL pe_find_section(struct image_file_map* fmap, const char* name, * * Unmaps a single section from memory */ -void pe_unmap_section(struct image_section_map* ism) +static void pe_unmap_section(struct image_section_map* ism) { if (ism->sidx >= 0 && ism->sidx < ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections && ism->fmap->u.pe.sect[ism->sidx].mapped != IMAGE_NO_MAP) @@ -163,7 +163,7 @@ void pe_unmap_section(struct image_section_map* ism) * * Get the RVA of an PE section */ -DWORD_PTR pe_get_map_rva(const struct image_section_map* ism) +static DWORD_PTR pe_get_map_rva(const struct image_section_map* ism) { if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections) return 0; @@ -175,13 +175,22 @@ DWORD_PTR pe_get_map_rva(const struct image_section_map* ism) * * Get the size of a PE section */ -unsigned pe_get_map_size(const struct image_section_map* ism) +static unsigned pe_get_map_size(const struct image_section_map* ism) { if (ism->sidx < 0 || ism->sidx >= ism->fmap->u.pe.ntheader.FileHeader.NumberOfSections) return 0; return ism->fmap->u.pe.sect[ism->sidx].shdr.Misc.VirtualSize; } +static const struct image_file_map_ops pe_file_map_ops = +{ + pe_map_section, + pe_unmap_section, + pe_find_section, + pe_get_map_rva, + pe_get_map_size, +}; + /****************************************************************** * pe_is_valid_pointer_table * @@ -206,11 +215,12 @@ static BOOL pe_is_valid_pointer_table(const IMAGE_NT_HEADERS* nthdr, const void* * * Maps an PE file into memory (and checks it's a real PE file) */ -static BOOL pe_map_file(HANDLE file, struct image_file_map* fmap, enum module_type mt) +BOOL pe_map_file(HANDLE file, struct image_file_map* fmap, enum module_type mt) { void* mapping; fmap->modtype = mt; + fmap->ops = &pe_file_map_ops; fmap->alternate = NULL; fmap->u.pe.hMap = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL); if (fmap->u.pe.hMap == 0) return FALSE;