1/* Test bug in dwfl_report_segment() coalescing.
2   Copyright (C) 2019 Facebook
3   This file is part of elfutils.
4
5   This file is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 3 of the License, or
8   (at your option) any later version.
9
10   elfutils is distributed in the hope that it will be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18#include <config.h>
19#include <assert.h>
20#include <inttypes.h>
21#include <stdio.h>
22#include <stdio_ext.h>
23#include <locale.h>
24#include ELFUTILS_HEADER(dwfl)
25
26
27static const Dwfl_Callbacks offline_callbacks =
28  {
29    .find_debuginfo = INTUSE(dwfl_standard_find_debuginfo),
30    .section_address = INTUSE(dwfl_offline_section_address),
31  };
32
33
34int
35main (void)
36{
37  /* We use no threads here which can interfere with handling a stream.  */
38  (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
39
40  /* Set locale.  */
41  (void) setlocale (LC_ALL, "");
42
43  Dwfl *dwfl = dwfl_begin (&offline_callbacks);
44  assert (dwfl != NULL);
45
46  GElf_Phdr phdr1 =
47    {
48      .p_type = PT_LOAD,
49      .p_flags = PF_R,
50      .p_offset = 0xf00,
51      .p_vaddr = 0xf00,
52      .p_filesz = 0x100,
53      .p_memsz = 0x100,
54      .p_align = 4,
55    };
56
57  int ndx = dwfl_report_segment (dwfl, 1, &phdr1, 0, dwfl);
58  assert(ndx == 1);
59
60  ndx = dwfl_addrsegment (dwfl, 0xf00, NULL);
61  assert(ndx == 1);
62
63  GElf_Phdr phdr2 =
64    {
65      .p_type = PT_LOAD,
66      .p_flags = PF_R | PF_W,
67      .p_offset = 0x1000,
68      .p_vaddr = 0x1000,
69      .p_filesz = 0x100,
70      .p_memsz = 0x100,
71      .p_align = 4,
72    };
73  ndx = dwfl_report_segment (dwfl, 2, &phdr2, 0, dwfl);
74  assert(ndx == 2);
75
76  ndx = dwfl_addrsegment (dwfl, 0x1000, NULL);
77  assert(ndx == 1 || ndx == 2);
78
79  dwfl_end (dwfl);
80
81  return 0;
82}
83