1#!/usr/local/bin/bash
2# Copyright (C) 2018 Red Hat, Inc.
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. $srcdir/test-subr.sh
19
20test_copy_and_add ()
21{
22  in_file="$1"
23  out_file="${in_file}.copy"
24  out_file_mmap="${out_file}.mmap"
25
26  testfiles ${in_file}
27  tempfiles ${out_file} ${out_file_mmap} readelf.out
28
29  # Can we copy the file?
30  testrun ${abs_builddir}/elfcopy ${in_file} ${out_file}
31  testrun ${abs_top_builddir}/src/elfcmp ${in_file} ${out_file}
32
33  # Can we add a section (in-place)?
34  testrun ${abs_builddir}/addsections 3 ${out_file}
35  testrun ${abs_top_builddir}/src/readelf -S ${out_file} > readelf.out
36  nr=$(grep '.extra' readelf.out | wc -l)
37  if test ${nr} != 3; then
38    # Show what went wrong
39    testrun ${abs_top_builddir}/src/readelf -S ${out_file}
40    exit 1
41  fi
42
43  # Can we add a section (in-place) using ELF_C_WRITE_MMAP?
44  testrun ${abs_builddir}/elfcopy --mmap ${in_file} ${out_file_mmap}
45  testrun ${abs_top_builddir}/src/elfcmp ${in_file} ${out_file_mmap}
46
47  # Can we add a section (in-place) using ELF_C_RDWR_MMAP?
48  # Note we are only adding one sections, adding more might fail
49  # because mremap cannot extend too much.
50  testrun ${abs_builddir}/addsections --mmap 1 ${out_file_mmap}
51  testrun ${abs_top_builddir}/src/readelf -S ${out_file_mmap} > readelf.out
52  nr=$(grep '.extra' readelf.out | wc -l)
53  if test ${nr} != 1; then
54    # Show what went wrong
55    testrun ${abs_top_builddir}/src/readelf -S ${out_file_mmap}
56    exit 1
57  fi
58}
59
60# A collection of random testfiles to test 32/64bit, little/big endian
61# and non-ET_REL (with phdrs)/ET_REL (without phdrs).
62
63# 32bit, big endian, rel
64test_copy_and_add testfile29
65
66# 64bit, big endian, rel
67test_copy_and_add testfile23
68
69# 32bit, little endian, rel
70test_copy_and_add testfile9
71
72# 64bit, little endian, rel
73test_copy_and_add testfile38
74
75# 32bit, big endian, non-rel
76test_copy_and_add testfile26
77
78# 64bit, big endian, non-rel
79test_copy_and_add testfile27
80
81# 32bit, little endian, non-rel
82test_copy_and_add testfile
83
84# 64bit, little endian, non-rel
85test_copy_and_add testfile10
86
87exit 0
88