| 1 |
/* Copyright (c) 2023, David Anderson |
| 2 |
All rights reserved. |
| 3 |
|
| 4 |
Redistribution and use in source and binary forms, with |
| 5 |
or without modification, are permitted provided that the |
| 6 |
following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above |
| 9 |
copyright notice, this list of conditions and the following |
| 10 |
disclaimer. |
| 11 |
|
| 12 |
Redistributions in binary form must reproduce the above |
| 13 |
copyright notice, this list of conditions and the following |
| 14 |
disclaimer in the documentation and/or other materials |
| 15 |
provided with the distribution. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 18 |
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 19 |
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 22 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 24 |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 28 |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 29 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
|
| 31 |
*/ |
| 32 |
#include <config.h> |
| 33 |
|
| 34 |
#include <stdlib.h> /* calloc() free() */ |
| 35 |
#include <string.h> /* memset() strcmp() strncmp() strlen() */ |
| 36 |
|
| 37 |
#if defined(_WIN32) && defined(HAVE_STDAFX_H) |
| 38 |
#include "stdafx.h" |
| 39 |
#endif /* HAVE_STDAFX_H */ |
| 40 |
|
| 41 |
#include "libdwarf_private.h" /* for TRUE FALSE */ |
| 42 |
#include "dwarf_secname_ck.h" |
| 43 |
|
| 44 |
/* Mainly important for Elf */ |
| 45 |
|
| 46 |
int |
| 47 |
_dwarf_startswith(const char * input,const char* ckfor) |
| 48 |
{ |
| 49 |
size_t cklen = strlen(ckfor); |
| 50 |
|
| 51 |
if (! strncmp(input,ckfor,cklen)) { |
| 52 |
return TRUE; |
| 53 |
} |
| 54 |
return FALSE; |
| 55 |
} |
| 56 |
|
| 57 |
static int |
| 58 |
_dwarf_startswithx(const char * input,const char* ckfor) |
| 59 |
{ |
| 60 |
size_t cklen = strlen(ckfor); |
| 61 |
int res = 0; |
| 62 |
|
| 63 |
res = strncmp(input,ckfor,cklen); |
| 64 |
return res; |
| 65 |
} |
| 66 |
/* If one uses the 'sort' command it ignores the proper place |
| 67 |
of . and _ so the output needs modification to get what we want. |
| 68 |
Here ensure _ and . before lowercase letters. |
| 69 |
If any here someday have uppercase ensure _ above |
| 70 |
uppercase letter. */ |
| 71 |
|
| 72 |
static const char *nonsec[] = { |
| 73 |
".bss", |
| 74 |
".comment", |
| 75 |
".data", |
| 76 |
".fini_array", |
| 77 |
".fini", |
| 78 |
".got", /* [5] */ |
| 79 |
".init", |
| 80 |
".interp", |
| 81 |
".jcr", |
| 82 |
".plt", |
| 83 |
".rel.data", /* 10 */ |
| 84 |
".rel.got", |
| 85 |
".rel.plt", |
| 86 |
".rel.text", |
| 87 |
".rela.data", |
| 88 |
".rela.got", /* 15 */ |
| 89 |
".rela.plt", |
| 90 |
".rela.text", |
| 91 |
".sbss", |
| 92 |
".text", /* [19] */ |
| 93 |
}; |
| 94 |
|
| 95 |
/* These help us ignore some sections that are |
| 96 |
irrelevant to libdwarf. */ |
| 97 |
int |
| 98 |
_dwarf_ignorethissection(const char *scn_name) |
| 99 |
{ |
| 100 |
int l = 0; |
| 101 |
int h = sizeof(nonsec)/sizeof(const char *) -1; |
| 102 |
int res = 0; |
| 103 |
|
| 104 |
while (l <= h) { |
| 105 |
int m = (l+h)/2; |
| 106 |
const char *s = nonsec[m]; |
| 107 |
|
| 108 |
res = _dwarf_startswithx(scn_name,s); |
| 109 |
if (res < 0) { |
| 110 |
h = m -1; |
| 111 |
} else if (res > 0) { |
| 112 |
l = m + 1; |
| 113 |
} else { |
| 114 |
return TRUE; |
| 115 |
} |
| 116 |
} |
| 117 |
return FALSE; |
| 118 |
} |