1#! /usr/bin/env bash
2# Copyright (C) 2019 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
20# Only run on 64bit systems, 32bit systems don't support > 4GB
21# ELF files.
22long_bit=$(getconf LONG_BIT)
23echo "long_bit: $long_bit"
24if test $long_bit -ne 64; then
25  echo "Only 64bit systems can create > 4GB ELF files"
26  exit 77
27fi
28
29# These tests need lots of disk space since they test files > 4GB.
30# Skip if there just isn't enough (2.5 * 4 = 10GB).
31space_available=$[$(stat -f --format="%a*%S" .)/(1024 * 1024 * 1024)]
32echo "space_available: $space_available"
33if test $space_available -lt 10; then
34  echo "Not enough disk space, need at least 10GB available"
35  exit 77
36fi
37
38# Make sure the files fit into memory, assume 6GB needed (2.5 * 2 + 1 extra).
39# Running under valgrind might need even more.
40mem_needed=6
41if [ "x$VALGRIND_CMD" != "x" ]; then
42  mem_needed=$[${mem_needed} + 2]
43fi
44echo "mem_needed: $mem_needed"
45mem_available=$(free -g 2>/dev/null | grep ^Mem: | awk -F ' +' '{print $7}')
46echo "mem_available: $mem_available"
47if test -z "$mem_available" || test $mem_available -lt $mem_needed; then
48  echo "Need at least ${mem_needed}GB free available memory"
49  exit 77
50fi
51
52# Make sure the disk is reasonably fast, should be able to write 100MB/s
53fast_disk=1
54timeout -s9 10s dd conv=fsync if=/dev/zero of=tempfile bs=1M count=1K \
55  || fast_disk=0; rm tempfile
56if test $fast_disk -eq 0; then
57  echo "File system not fast enough, need at least 100MB/s"
58  exit 77
59fi
60
61# NOTE: test file will be mangled and removed!
62test_file ()
63{
64  in_file="$1"
65  readelf_out="${in_file}.readelf.out"
66  out_file_strip="${in_file}.strip"
67  out_file_debug="${in_file}.debug"
68
69  testfiles ${in_file}
70  tempfiles ${readelf_out} ${out_file_mmap} ${out_file_strip} ${out_file_debug}
71
72  # Add two 2GB sections to the file.
73  echo "addsections 2 ${in_file} 2147483648"
74  testrun ${abs_builddir}/addsections 2 ${in_file} 2147483648
75  testrun ${abs_top_builddir}/src/readelf -S ${in_file} > ${readelf_out}
76  nr=$(grep '.extra' ${readelf_out} | wc -l)
77  if test ${nr} != 2; then
78    # Show what went wrong
79    cat ${readelf_out}
80    exit 1
81  fi
82
83  echo "strip -o ${out_file_strip} -f ${out_file_debug} ${in_file}"
84  testrun ${abs_top_builddir}/src/strip -o ${out_file_strip} \
85                                        -f ${out_file_debug} ${in_file}
86
87  echo "elflint --gnu ${out_file_strip}"
88  testrun ${abs_top_builddir}/src/elflint --gnu ${out_file_strip}
89
90  echo "elflint --gnu -d ${out_file_debug}"
91  testrun ${abs_top_builddir}/src/elflint --gnu -d ${out_file_debug}
92
93  # Now test unstrip recombining those files.
94  echo "unstrip ${out_file_strip} ${out_file_debug}"
95  testrun ${abs_top_builddir}/src/unstrip ${out_file_strip} ${out_file_debug}
96
97  echo "elfcmp ${out_file} ${out_file_strip}"
98  testrun ${abs_top_builddir}/src/elfcmp ${in_file} ${out_file_debug}
99
100  # Remove the temp files immediately, they are big...
101  rm -f ${in_file} ${out_file_strip} ${out_file_debug}
102}
103
104# A collection of random testfiles to test 64bit, little/big endian
105# and non-ET_REL (with phdrs)/ET_REL (without phdrs).
106# Don't test 32bit, they cannot go beyond 4GB.
107
108# 64bit, little endian, rel
109test_file testfile38
110
111# 64bit, big endian, non-rel
112test_file testfile27
113
114exit 0
115